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

  • Flask Framework Cookbook
  • Shalabh Aggarwal
  • 559字
  • 2021-08-05 17:17:19

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:

主站蜘蛛池模板: 锦州市| 北安市| 班玛县| 平乡县| 松溪县| 论坛| 兴城市| 秦安县| 蒙阴县| 朝阳区| 桦南县| 阜新市| 库尔勒市| 凤台县| 雷山县| 长宁区| 托克托县| 富锦市| 灵丘县| 大关县| 荆门市| 铜山县| 稻城县| 宁海县| 台中市| 平江县| 黑龙江省| 郧西县| 宁强县| 南阳市| 西乌珠穆沁旗| 鸡东县| 中卫市| 平和县| 武安市| 茶陵县| 阿合奇县| 明溪县| 海南省| 交城县| 平顶山市|