官术网_书友最值得收藏!

Environment setup with virtualenv

Flask can be installed using pip or easy_install globally, but we should always prefer to set up our application environment using virtualenv. This prevents the global Python installation from getting affected by our custom installation by creating a separate environment for our application. This separate environment is helpful because you can have multiple versions of the same library being used for multiple applications, or some packages might have different versions of the same libraries as dependencies. virtualenv manages this in separate environments and does not let a wrong version of any library affect any application.

How to do it…

We will first install virtualenv using pip 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:

$ pip install virtualenv
$ virtualenv my_flask_env

Now, from inside the my_flask_env folder, we will run the following commands:

$ cd my_flask_env
$ source bin/activate
$ pip 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…

Until now, we have used pip 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 pip, we will see that a number of packages are installed. The following is a summary of the package installation process of Flask:

$ pip install -U flask
Downloading/unpacking flask
…........
…........
Many more lines.........
…........
Successfully installed flask Werkzeug Jinja2 itsdangerous markupsafe
Cleaning up...

Note

In the preceding command, -U refers to the installation with upgrades. This will overwrite the existing installation (if any) with the latest released versions.

If we notice carefully, there are five packages installed in total, namely flask, Werkzeug, Jinja2, 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 virtualenv easier.

Tip

Remember that the installation of virtualenvwrapper should be done at a global level. So, deactivate any virtualenv that might still be active. To deactivate it, just use the following command:

$ deactivate

Also, it is possible that you might not be able to install the package at a global level because of permission issues. Switch to superuser or use sudo in this case.

You can install virtualenvwrapper using the following commands:

$ pip 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 our virtual environments created using virtualenvwrapper. To install Flask, use the following commands:

$ mkvirtualenv flask
$ pip install flask

To deactivate a virtualenv, we can just run the following command:

$ deactivate

To activate an existing virtualenv using virtualenvwrapper, we can run the following command:

$ workon flask

See also

References and installation links are as follows:

主站蜘蛛池模板: 乐山市| 泽库县| 文安县| 福海县| 玉山县| 民勤县| 密云县| 靖宇县| 白水县| 元朗区| 九龙县| 临洮县| 黎川县| 昌邑市| 东宁县| 汶上县| 西昌市| 渭源县| 通道| 西吉县| 收藏| 故城县| 赞皇县| 辽宁省| 鄄城县| 东海县| 绥滨县| 乌拉特后旗| 南陵县| 荆州市| 洛南县| 屏东市| 岳阳市| 芷江| 福清市| 元朗区| 安庆市| 中江县| 灯塔市| 宜城市| 延长县|