Flask Tutorials: Environment Variables (FLASK_APP, FLASK_ENV)

beginner
17 min

Flask Tutorials: Environment Variables (FLASK_APP, FLASK_ENV)

Welcome to our Flask Environment Variables tutorial! In this lesson, we'll delve into two essential environment variables - FLASK_APP and FLASK_ENV - that are crucial for managing your Flask applications effectively. Let's get started! 🎯

What are Environment Variables?

Environment variables are key-value pairs that store configuration data for applications. They can be set at various levels, including the operating system, the user, and the application itself. These variables provide a flexible way to configure application behavior without modifying the code directly. 📝

Understanding FLASK_APP and FLASK_ENV

FLASK_APP

FLASK_APP is an environment variable used by Flask to determine the application's entry point. This entry point is typically a Python module that initializes your Flask app. The value of this variable should point to the name of the Python file (without the .py extension) that contains your Flask app's main function. 💡 Pro Tip: The application's entry point can be specified either as an environment variable or directly on the command line.

Example 1: Using FLASK_APP as an environment variable

python
# app.py from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello, World!' if __name__ == '__main__': app.run()

To run the above code, first, set the FLASK_APP environment variable:

sh
$ export FLASK_APP=app

Then, start the Flask development server:

sh
$ flask run

FLASK_ENV

FLASK_ENV is another environment variable used by Flask to configure the application's environment mode. The supported values are development, testing, and production. Setting this variable allows you to switch between different environments easily, each with its own configuration settings. 💡 Pro Tip: In a production environment, it's essential to use environment-specific configuration files.

Example 2: Using FLASK_ENV to switch environments

python
# config.py class DevelopmentConfig(object): DEBUG = True class ProductionConfig(object): DEBUG = False config = { 'development': DevelopmentConfig, 'production': ProductionConfig, } # app.py from flask import Flask, current_app from flask.ext.config import Config app = Flask(__name__) app.config.from_object(config['development']) @app.route('/') def hello(): if current_app.debug: return 'Hello, World! (Debug mode)' else: return 'Hello, World! (Production mode)'

To run the above code in the development environment, first, set the FLASK_APP and FLASK_ENV environment variables:

sh
$ export FLASK_APP=app $ export FLASK_ENV=development

Then, start the Flask development server:

sh
$ flask run

Quiz

Quick Quiz
Question 1 of 1

Which environment variable is used by Flask to determine the application's entry point?

Conclusion

Environment variables play a crucial role in managing Flask applications efficiently. Familiarizing yourself with FLASK_APP and FLASK_ENV will help you switch between different environments, configure your app settings, and simplify your development workflow.

With these concepts under your belt, you're now one step closer to becoming a proficient Flask developer! ✅

Stay tuned for more Flask tutorials coming soon on CodeYourCraft. Happy coding! 💡