Flask can be simply installed using pip/pip3 or easy_install globally, but it's preferable to set up an application environment using virtualenv. 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 used for multiple applications; some packages might also have different versions of the same libraries as dependencies. virtualenv manages this in separate environments and does not let the incorrect version of any library affect any application. In this recipe, we will learn about how to create and manage these environments.
Setting up our environment with virtualenv
How to do it...
First, install virtualenv using pip3 and then create a new environment with the name my_flask_env inside the folder in which we ran the first command. This will create a new folder with the same name, as follows:
$ pip3 install virtualenv $ virtualenv my_flask_env
Run the following commands from inside the my_flask_env folder:
$ cd my_flask_env $ source 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 the 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 a number of packages are installed. The following is an outline of the package installation process of Flask:
$ pip3 install -U flask Downloading/unpacking flask ........... ........... Many more lines......... ........... Successfully installed flask Werkzeug Jinja2 itsdangerous
markupsafe click Cleaning up...
If we look carefully at the preceding snippet, we will see that there are six packages installed in total; namely, flask, Werkzeug, Jinja2, click, itsdangerous, and markupsafe. These are the packages on which Flask depends, and it will not work if any of them are missing.
There's more...
To make our lives easier, we can use virtualenvwrapper, which, as the name suggests, is a wrapper written over virtualenv and makes the handling of multiple instances of virtualenv easier.
Remember that the installation of virtualenvwrapper should be done on a global level. So, deactivate any virtualenv that might still be active. To deactivate, use the following command:
$ deactivate
Install virtualenvwrapper using the following commands:
$ pip3 install virtualenvwrapper $ export WORKON_HOME=~/workspace $ source /usr/local/bin/virtualenvwrapper.sh
In the preceding code, we installed virtualenvwrapper, created a new environment variable with the name WORKON_HOME, and provided it with a path, which will act as the home for all virtual environments created using virtualenvwrapper.
To create a virtualenv and install Flask, use the following commands:
$ mkvirtualenv my_flask_env $ pip3 install flask
To deactivate a virtualenv, simply run the following command:
$ deactivate
To activate an existing virtualenv using virtualenvwrapper, run the following command:
$ workon my_flask_env
See also
The references and installation links relating to this section are as follows: