Realizing development isolation with virtual environment

Posted by thebadbad on Fri, 28 Jan 2022 06:21:44 +0100

★ this article is a selection of manuscripts being written. Let's have a preview. "

11.5 creating a virtual environment

In the actual project, is it necessary to use the "latest version" module or package? not always. The actual project requirements are often complex. For example, there is an "ancient" website project that uses Django 2.2 (see Section 12.3 of Chapter 12). Now there is a new website that requires Django 3. Thus, in the development environment of the local computer, there are different version conflicts of the same package. How to solve them?

We hope that each project has a relatively independent development environment, which is isolated from the system configuration and the configuration of other projects, so that we can "do whatever we want" in the project. This relatively independent development environment is the Virtual Environment in Python.

The module venv for creating a virtual environment has been provided in the Python standard library. The following applies this module to demonstrate the process of creating a virtual environment.

A virtual environment is represented as a directory, which must be created first. In the following demonstration, you are going to put the directory of the virtual environment in / Users/qiwsir/Documents/my_books/codes, and the name of the virtual environment directory is myvenv. Then execute:

 % python -m venv /Users/qiwsir/Documents/my_books/codes/myvenv

In order to avoid such a long path, you can enter the directory first/ codes, and then execute:

 % python -m venv myvenv

Also in/ A subdirectory named myvenv is created in the codes directory, which is the virtual environment directory.

Enter the myvenv subdirectory:

qiwsir@qiwsirs-MacBook-Pro codes % cd myvenv
qiwsir@qiwsirs-MacBook-Pro myvenv % ls
bin  include  lib  pyvenv.cfg

Command ls is a Linux command, that is, to view the files and subdirectories in this directory (users using Windows operating system cannot copy this command, use dir instead, and the displayed directory name may be slightly different). It is found that this directory is not empty, but has basic configuration.

In the subdirectory bin (Scripts in Windows system), you can see the following:

qiwsir@qiwsirs-MacBook-Pro myvenv % cd bin
qiwsir@qiwsirs-MacBook-Pro bin % ls
Activate.ps1  activate.csh  easy_install  pip   pip3.9   python3
activate  activate.fish  easy_install-3.9 pip3   python   python3.9

This indicates that Python 3.9 has been configured for this virtual environment, because when creating the virtual environment, the instruction python is Python 3.9.

In the subdirectory lib, readers will find/ python3. 9 / site packages subdirectory (LIB \ site packages on Windows system). All third-party packages installed in this virtual environment in the future will be placed here.

Start the virtual environment (it cannot be used without startup) and perform the following operations (currently in. / myeven directory):

qiwsir@qiwsirs-MacBook-Pro myvenv % source ./bin/activate
(myvenv) qiwsir@qiwsirs-MacBook-Pro myvenv %

Source is executed/ After the bin / activate command, the (myvenv) appears in front of the current command line, indicating that you have entered (or started) the myvenv virtual environment.

(myvenv) qiwsir@qiwsirs-MacBook-Pro myvenv % pip list
Package    Version
---------- -------
pip        20.2.3
setuptools 49.2.1
WARNING: You are using pip version 20.2.3; however, version 21.2.1 is available.
You should consider upgrading via the '/Users/qiwsir/Documents/my_books/codes/myvenv/bin/python -m pip install --upgrade pip' command.

It is found from the above operations that other modules have not been installed in the current virtual environment except the two listed, and the pip version in this environment is 20.2.3. In section 11.4.1, the pip installed in the local computer system has been upgraded to 21.2.1, and here is the default pip version of Python 3.9. It can be seen that the virtual environment is isolated from the system environment.

(myvenv) qiwsir@qiwsirs-MacBook-Pro myvenv % pip install django
Collecting django
  Downloading Django-3.2.5-py3-none-any.whl (7.9 MB)
     |████████████████████████████████| 7.9 MB 1.1 MB/s
Collecting pytz
  Using cached pytz-2021.1-py2.py3-none-any.whl (510 kB)
Collecting asgiref<4,>=3.3.2
  Downloading asgiref-3.4.1-py3-none-any.whl (25 kB)
Collecting sqlparse>=0.2.2
  Downloading sqlparse-0.4.1-py3-none-any.whl (42 kB)
     |████████████████████████████████| 42 kB 509 kB/s
Installing collected packages: pytz, asgiref, sqlparse, django
Successfully installed asgiref-3.4.1 django-3.2.5 pytz-2021.1 sqlparse-0.4.1

Django 3.2.5 is installed in this virtual environment - remember this installation method, which will be used in Section 12.3 of Chapter 12.

If it's not in the myvenv directory, does it mean you quit the virtual environment?

(myvenv) qiwsir@qiwsirs-MacBook-Pro myvenv % cd
(myvenv) qiwsir@qiwsirs-MacBook-Pro ~ %

No, as long as the (myvenv) flag is also displayed in the front, it means that it is still in the virtual environment, no matter in which directory, even in the location shown above, if a third-party package is installed, it will also be installed in the virtual directory.

(myvenv) qiwsir@qiwsirs-MacBook-Pro ~ % pip install Flask

Install another framework commonly used for web project development, flash, and then check the third-party packages already available in the current virtual environment.

(myvenv) qiwsir@qiwsirs-MacBook-Pro ~ % pip list
Package      Version
------------ -------
asgiref      3.4.1
click        8.0.1
Django       3.2.5
Flask        2.0.1
itsdangerous 2.0.1
Jinja2       3.0.1
MarkupSafe   2.0.1
pip          20.2.3
pytz         2021.1
setuptools   49.2.1
sqlparse     0.4.1
Werkzeug     2.0.1

Although only two packages were installed in the previous demonstration, because both packages are web development frameworks, they also have some dependent packages that are automatically installed together. The above shows that there are packages and modules in the current virtual environment - which is far less than the packages installed in the local computer system.

With a relatively independent environment, the development in this environment can avoid the interference between different projects. For more detailed development process, refer to relevant chapters in Chapter 12.

When the work in the virtual environment is finished, you must exit. The basic operations are:

(myvenv) qiwsir@qiwsirs-MacBook-Pro ~ % deactivate
qiwsir@qiwsirs-MacBook-Pro ~ %

Now there is no (myvenv) tag and we are back in the system environment.

★ self study suggestions So far, readers have learned the basic knowledge of Python. Although these knowledge can support general project development, I think the focus is not on this, but on the cultivation and improvement of self-study ability through learning with the help of this book. Since then, readers have confidence and ability to quickly master any new knowledge encountered in the project. "