Setting up a virtual environment
Flask can be simply installed using pip
/pip3
or easy_install
globally, but it’s preferable to set up an application environment using venv. This prevents the global Python installation from being affected by a custom installation as it creates a separate environment for the application. This separate environment is helpful as it allows you to have multiple versions of the same library for multiple applications; some packages might also have different versions of the same libraries as dependencies. venv
manages this in separate environments and does not let the incorrect version of any library affect any application. In this recipe, we will learn how to create and manage these environments.
How to do it...
Python3 comes bundled with a venv
module to create virtual environments. So, simply create a new environment called my_flask_env
(or any other name of your choice) inside the folder of your choice where you want your development environment to live. This will create a new folder with the same name, as follows:
$ python3 -m venv my_flask_env
Run the following commands from inside the my_flask_env
folder:
$ source my_flask_env/bin/activate $ pip3 install flask
This will activate our environment and install flask
inside it. Now, we can do anything with our application within this environment, without affecting any other Python environment.
How it works...
So far, we have used pip3 install flask
multiple times. As its name suggests, the command refers to the installation of Flask, just like any Python package. If we look a bit deeper into the process of installing Flask via pip3
, we will see that several packages are installed. The following is an outline of the package installation process of Flask:
$ pip3 install flask Collecting Flask ........... ........... Many more lines......... ........... Installing collected packages: zipp, Werkzeug, MarkupSafe, itsdangerous, click, Jinja2, importlib-metadata, Flask Successfully installed Flask-2.1.2 Jinja2-3.1.2 MarkupSafe-2.1.1 Werkzeug-2.1.2 click-8.1.3 importlib-metadata-4.11.4 itsdangerous-2.1.2 zipp-3.8.0
If we look carefully at the preceding snippet, we will see that multiple packages have been installed. Of these, five packages, namely, Werkzeug
, Jinja2
, click
, itsdangerous
, and markupsafe
, are the packages on which Flask depends, and it will not work if any of them are missing. Others are sub-dependencies that are needed for the dependencies of Flask to work.
There’s more...
Before venv
was introduced in Python 3.3, virtualenv
was the standard library used to create and manage virtual environments. venv
is a subset of virtualenv
and misses out on the advanced features that virtualenv
provides. For the sake of simplicity and to stay in the context of this book, I will use venv
, but you are free to explore virtualenv
and virtualenvwrapper
.
See also
The references relating to this section are as follows:
- https://pypi.python.org/pypi/Flask
- https://pypi.python.org/pypi/Werkzeug
- https://pypi.python.org/pypi/Jinja2
- https://pypi.python.org/pypi/itsdangerous
- https://pypi.python.org/pypi/MarkupSafe
- https://pypi.python.org/pypi/click
Read more about virtualenv
and virtualenvwrapper
at https://virtualenv.pypa.io/en/latest/ and https://pypi.org/project/virtualenvwrapper/.