DEV Community

Cover image for Installing Flask and creating a virtual environment
Graham Patrick
Graham Patrick

Posted on

Installing Flask and creating a virtual environment

Before we can start working on our Flask app, we need to install Flask and create a virtual environment. A virtual environment is a way of isolating the dependencies of our project from the rest of the system, so that we can avoid conflicts and ensure reproducibility.

To install Flask, we can use the pip package manager. Pip is a tool that allows us to install and manage Python packages from the Python Package Index (PyPI). To install Flask, we can run the following command in the terminal:

pip install flask
Enter fullscreen mode Exit fullscreen mode

This will download and install Flask and its dependencies. You can check the version of Flask that you have installed by running:

flask --version
Enter fullscreen mode Exit fullscreen mode

You should see something like this:

Flask 2.0.2
Python 3.9.7
Enter fullscreen mode Exit fullscreen mode

To create a virtual environment, we can use the venv module that comes with Python. Venv is a tool that creates a directory that contains a copy of the Python interpreter and the packages that we need for our project. To create a virtual environment, we can run the following command in the terminal:

python -m venv venv
Enter fullscreen mode Exit fullscreen mode

This will create a directory called venv in the current working directory. To activate the virtual environment, we can run the following command in the terminal:

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

This will change the prompt to indicate that we are in the virtual environment. You should see something like this:

(venv) $
Enter fullscreen mode Exit fullscreen mode

Now, we can install Flask and any other packages that we need for our project in the virtual environment. To deactivate the virtual environment, we can run the following command in the terminal:

deactivate
Enter fullscreen mode Exit fullscreen mode

This will return us to the normal prompt. You should see something like this:

$
Enter fullscreen mode Exit fullscreen mode

Original Post: https://enceladus-software.com/post/149

Top comments (1)

Collapse
 
proteusiq profile image
Prayson Wilfred Daniel • Edited

The order of installation 🤗 ensuring flask installation on correct environment;

python -m venv .venv
source .venv/bin/activate

pip install flask
Enter fullscreen mode Exit fullscreen mode

For me, I have set

export PIP_REQUIRE_VIRTUALENV=true
Enter fullscreen mode Exit fullscreen mode

on my .bashrc to force me to have environments before installing packages 📦