Wen: Sun snow
Source: Python Technology [public ID: pythonall]

What makes Python powerful is not only the features of the language itself, but also its extensive third-party libraries. A powerful software library that allows developers to focus on their business and avoid the waste of reinventing the wheel. However, the complex dependencies created by numerous software libraries and the long-running Python2 / Python3 conflict have caused a lot of problems for Python projects. So Python recommends using virtual environment tools to create clean dependencies for projects. Today we’ll take a look at the Python virtual environment

Some of the concepts

Python virtual environments, with their many concepts and tools, can be fascinating and frustrating to use, so let’s take a look at some of the concepts and tools associated with them

Python version

The Python version refers to the version of the Python parser itself. Python3 is not compatible with Python2, and the conflict between the two camps lasted for a long time, resulting in some software libraries requiring two versions of Python, and developers may need to deploy different versions of Python in the same environment, causing problems for development and maintenance. Hence Pyenv, a version manager similar to NodeJs-like NVM, which can create isolated Python environments and easily switch Python versions within the environment, but with little to do with the Python virtual environment

Python package library

Pypi (Python Package Index) pypI.org/ is the official designated Package library. This is where the PIP tool based on it finds and downloads installation packages. In order to improve the download speed, the world has a lot of Pypi mirror server, at home there are many software sources, such as ali’s software sources are: mirrors.aliyun.com/pypi/simple… . In addition, there are other software sources, such as Repo.anaconda.com/, anaconda’s software source for scientific computing

Python package manager

There are a large number of software packages in the software source and various versions. Therefore, you need to use software source management tools, such as PIP, Conda, Pipenv, and Poetry

  • PIP is the most commonly used package management tool. The PIP install command is used to install software packages using the PYPI software source

  • Conda is widely used as a package management tool in the field of scientific computing, with rich and powerful functions. The software package sources are Anaconda Repository and Anaconda Cloud. Conda not only supports Python software packages, You can also install two custom packages for C, C++, R, and other languages. In addition to package management, it provides an isolated software environment.

  • Pipenv is a Python dependency management tool published by Kenneth Reitz in January 2017 and now maintained by PyPA. Pipenv automatically manages virtual environments and dependency files for you, and provides a series of commands and options to help you implement various dependency and environment management operations

  • Poetry, like Pipenv, is a Python virtual environment and dependency management tool that also provides package management capabilities such as packaging and publishing. You can think of it as a superset of tools like Pipenv and Flit. It allows you to use Poetry to manage both Python libraries and Python programs

Many package management tools not only provide basic package management functions, but also provide virtual environment construction, program management and other functions

Python Virtual Environment

Python applications often need to use packages, third-party packages or modules, sometimes depending on a particular version of a package or library, so you can’t have a software environment that fits all Python applications. Many times, different Python applications rely on versions that conflict, and if one is satisfied, the other won’t work. The solution to this problem is virtual environments. A virtual environment is a self-contained directory that contains specific Python parsers and packages. Different applications can use different virtual environments to solve dependency conflicts, and virtual environments only need to install application-related packages or modules to facilitate deployment

Building a Virtual Environment

The principle of

Virtual environments are not a new technology, but take advantage of environment variables in operating systems and the isolation of environment between processes

Environment variables of the operating system can provide the information for application and information exchange medium, the process can be Shared in the operating system environment variables, can also specifies the environment variables for the process, in which the PATH environment variable, it is very important for the operating system and program provides access PATH for executable files, for example, write a program a.e xe, The D:\MyProgram directory is stored in the D:\MyProgram directory. If you run a.exe on the command line, you will get a message saying “program A.exe cannot be found”. You can add the D:\MyProgram directory to the PATH environment variable. The operating system will look through each of the paths provided by PATH, and it will find it. Linux and MacOS have similar features, even more than Windows.

The Python virtual environment is built using this feature. When activating the virtual environment, the activation script changes the PATH of the current command line program to the virtual environment. In this way, the execution commands will be looked up in the modified PATH, avoiding commands that could have been found by PATH. This enables isolation of the Python environment.

To make it easy for developers to tell if the current environment is a virtual environment and which virtual environment it is, the command prompt is preceded by special flags such as:

create

Virtualenv tool

Before Python3.3, you could only create virtual environments with Virtualenv, which you would need to install first

pip install virtualenv
Copy the code

After the installation, create a virtual environment called Myvenv in the current directory:

virtualenv --no-site-packages myvenv
Copy the code

The no-site-packages parameter is used to create a “clean” virtual environment without copying third-party packages installed in the main environment

Virtualenv also has many parameters for different usage scenarios, such as:

  • -p: Specifies the Python parser, which is the installed Python application. The default is Python in the current environment
  • –no-pip: PIP does not need to be installed
  • –clear: If the directory in which the virtual environment is created already has other virtual environments, rebuild the virtual environment

Venv module

After Python3.3, you can use the venv module to replace the virtualenv tool. The advantage is that you do not need to install the virtualenv tool separately. In versions 3.3 and later, you can create virtual environments using Python installed:

python -m venv myvenv
Copy the code

You can create a virtual environment called Myvenv in the current directory

Venv has a few parameters, but not as many as Virtualenv. Here are a few:

  • –without-pip: PIP does not need to be installed
  • –clear: If the directory in which the virtual environment is created already has other virtual environments, rebuild the virtual environment

Because Venv is built with a Python parser attached, there is no need to specify the Python interpreter version

The activation

After a virtual environment is created, you need to activate it to use it in the current command line. In this way, you can replace the value of PATH variable in the current command line environment

Virtual environments created with VirtualEnv and the module Venv are activated in the same way: run the activation script

  • On Windows, the activation script path is

    \Scripts\activate.bat. On the powershell command line, change the script to activate. ps1

  • On Linux, the activation script path is

    /bin/activate. By default, the script does not have execution permission. You can either set the script to executable or run it using the source command, for example

    $ source myvenv/bin/activate
    Copy the code

When activated, virtual environment markers are visible on the command line, as shown in the figure above

To print PATH, run the following command:

Linux under:

echo $PATH
Copy the code

Under Windows

echo %PATH%
Copy the code

You can see that the created virtual environment script directory is loaded first

exit

Deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate deactivate

Exiting the virtual environment restores the PATH to its original state

Coordinate with development tools

Although it is easy to switch to a virtual environment by activating the script, it is still not convenient in practical development, and many development tools, especially those that provide Python parsing environment, can work with the virtual environment, making the development process almost useless, which is a great help to the development work

Visual Studio Code

VS Code is an up-and-comer with powerful features and a wealth of plugins, making it the fastest growing integrated development tool of the past two years. In the current version, configuring the Python virtual environment is as simple as selecting a Python interpreter

At the same time, press Ctrl+Shift+P, enter “parser” in the command window that will pop up, and select “Python: Select Parser “from the drop-down list. This will cache some parsers that have been created. If you don’t want any, You can create a new parser by selecting “Enter Interpreter Path “to select the parser path, which is the Python program in the created virtual environment script folder

If you are editing a Python code file, you can also select and switch the interpreter in the status bar, which is more convenient

Pycharm

Pycharm is a Python development tool with the best functions. It has been converted to Python development. In addition to basic development functions, Pycharm also provides project creation, packaging, testing and other rich functions, and has a large market share

When creating a project, in the project Creation dialog box, you can create or select an existing parser

When choosing to create a new parser, you need to select a tool to create a virtual environment, such as Virtualenv. Specify the directory for the virtual environment. Select the Python base parser, the same effect as the -p parameter of the Virtualenv tool; And whether to inherit the third-party libraries of the underlying parser and use this virtual environment as the default for creating other projects

If you select an existing parser, like VS Code, you can select one that is already cached or specify the path to the parser

Deploying a Virtual Environment

In addition to avoiding conflicts between libraries, virtual environment is chosen for convenient deployment. Virtual environment is independent and only contains project-related dependent libraries, so deployment is more efficient and less risky

The general deployment process is as follows:

  1. After development is complete, usepip freeze > requirements.txtThe command exports the project’s library dependencies as part of the code
  2. Upload the code to the server
  3. Create a virtual environment on the server
  4. To activate the virtual environment, runpip install -r requirements.txt, install project dependencies

How do you run a project, depending on the project

  • The Web project

Web projects usually use Django, Flask Web development to provide Web services projects, deployment requires a Web container, as the program running environment, container configuration has a virtual environment Settings, in fact, is specified Python parser path. Just set it up as a directory or Python parser for the virtual environment, which is used at startup and isolated from other environments. For example, the HOME parameter in the uWSGI configuration file is used to specify the parser.

  • Service items

Service projects need to run for a long time in the form of services, such as the bus alarm clock introduced before, or some timing crawlers, etc. For Linux, the current mainstream service mode is Systemd, which is a more advanced service management tool than init. In the service script, Set ExecStart to execute commands for the Python parser of the full path virtual environment, which is used independently when the service is started. Windows services, for EXE packages, do not need to configure the virtual environment, because the packaging has taken into account the environment, if the script is running, you need to specify a full path Python parser

  • Run the project once

For some test or experimental projects, this is mostly done manually, as long as you run it in an active virtual environment or with a specific Python parser, not much different from running it during development

Other virtual environment management tools

  • Virtualenvwrapper: a wrapper around virtualenv and an extension for vim and emacs users that supports bash/ KSH/ZSH
  • Virtualenvwrapper -win: Virtualenvwrapper for Windows Batch shell
  • Pyenv: used to solve such problems. It can install, uninstall, compile, manage multiple versions of Python, and set one of them up as a working environment at any time
  • Pyenv-win: Pyenv for Windows

You can try it out if you’re interested

conclusion

Today I’ve looked at the concepts and tools of the Python virtual environment, and briefly described some of the ways in which it can be used in the development process. Limited by space, we can’t expand on more details. We need to try more in practice. Just DO It!

reference

  • Docs.python.org/3/tutorial/…
  • Greyli.com/back-to-vir…
  • www.jianshu.com/p/dcb281ee5…
  • Blog.csdn.net/SpuerCheng/…
  • www.cnblogs.com/qinhan/p/92…

Welcome to follow the wechat official account: Python technology, here we have personally written 100 days of actual practice, there are all kinds of interesting programming practice, there are all kinds of learning materials, and a large group of lovely friends to discuss with each other.