4 minutes
Setting Up A Flask App With A Virtual Environment
Flask is a lightweight and powerful web framework for Python, making it a popular choice for web development. Virtual environments are crucial for Python projects because they allow you to manage dependencies separately for each project, avoiding conflicts. In this post, I’ll walk you through setting up a Flask app with a virtual environment from scratch.
1. Install Python
Before diving in, ensure that Python is installed on your system. You can verify this by opening your terminal or command prompt and running the following command:
python --version
Or, depending on your system:
python3 --version
If Python is not installed, head over to the official Python website and download the latest version.
2. Set Up a Project Directory
Next, let’s create a directory for your Flask project. This directory will hold all your project files.
mkdir my_flask_app
cd my_flask_app
Here, my_flask_app
is the name of your project directory. Feel free to name it anything you like.
3. Create a Virtual Environment
You can create a virtual environment either through the terminal or VSCode. When created through VSCode, the environment can automatically activate for the project, making it convenient to switch without manual activation.
**Using the Create Environment command** In VSCode
Open the Command Palette ( ⇧⌘P ), search for the Python: Create Environment command, and select it.
Select Venv,
And you will be presented a list of interpreters that can be used as a base for the new virtual environment.
In the Terminal
To create a virtual environment inside your project directory, run the following command:
On Windows:
python -m venv .venv
On macOS/Linux:
python3 -m venv .venv
This command creates a new directory named venv
inside your project directory, which will contain the virtual environment.
To start using your virtual environment, you need to activate it. The activation command differs depending on your operating system:
On Windows:
.venv\Scripts\activate
On macOS/Linux:
source .venv/bin/activate
Once activated, you’ll notice that your terminal prompt is prefixed with (venv)
, indicating that your virtual environment is active.
Also, you will see the selected interpreted version on the right side of the Status Bar.
5. Install Flask
With your virtual environment activated, the next step is to install Flask. This can be done easily using pip
, Python’s package manager:
pip install Flask
This will install Flask and its dependencies in your virtual environment.
6. Create a Simple Flask App
Now that Flask is installed, let’s create a simple Flask application. In your project directory, create a new file named app.py
and add the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, Flask!"
if __name__ == '__main__':
app.run(debug=True)
This basic Flask app displays “Hello, Flask!” when you visit the home page (/
).
7. Run the Flask App
To see your Flask app in action, run the following command in your terminal:
python app.py
Note: If your app file isn’t named app.py
, set the environment variable FLASK_APP=<filename>
before running flask run
.
Alternatively, you can use the Flask CLI:
flask run
Your Flask app should now be running locally on http://127.0.0.1:5000/
. Open this URL in your web browser, and you should see the “Hello, Flask!” message.
8. Deactivate the Virtual Environment
If you’re finished working in this environment, you can deactivate it by running:
deactivate
This will return your terminal to its normal state, where the virtual environment is no longer active.
9. Save Your Dependencies
It’s a good practice to save your project’s dependencies in a requirements.txt
file. This file allows you to easily recreate the same environment later or share it with others. To generate this file, run:
pip freeze > requirements.txt
Later, you or anyone else can install these dependencies in a new environment using:
pip install -r requirements.txt
Conclusion
Setting up a Flask app with a virtual environment is a straightforward process that ensures your project remains clean and manageable. By following these steps, you can easily create isolated environments for your Flask projects, helping you avoid conflicts and manage dependencies efficiently.
Now that you have a basic Flask app running, you’re ready to start building more complex applications. Happy coding!