Master the Python virtual environment in one article - improve your development efficiency

Before actually starting Python code writing, compiling, running, debugging and developing projects, you must understand the configuration of Python's virtual environment. After you are familiar with using it, it will greatly improve the subsequent development efficiency and reduce problems caused by non-code reasons. virtualenv is the Virtual Environment-virtual environment in Python. This article will cover the basics of Python virtual environments, how to use them, and how they work.

1 Basic knowledge of virtual environment

One of the biggest reasons for Python's massive popularity among developers is its wide and ever-expanding selection of third-party packages. The virtual environment can create a separate isolation environment for each project to achieve the purpose of separating the dependencies of different projects.

Python has various modules and packages for different applications. In our project development, some three-party libraries may be needed, so we need to install this library, and another project may not need any other third-party packages, or other versions of some of the three-party packages. Problems arise when different Python projects require competing or incompatible versions of the same plugin, causing programs to behave inexplicably.

In addition, the computer used for development may have Python2 and Python3 installed at the same time, and Python2 and Python3 have more minor version numbers, and some ready-made projects may be developed earlier and only support Python2 compilation, while others must be Python3. OK, so if there is no isolation, it will also cause the program to fail to run.

Therefore, virtual environments can come into play at this point, creating separate isolated environments for different Python installations, or different projects, each project can store and retrieve packages from its specific environment, using a specific Python version.

Two how to use the virtual environment

Here, an example is used to illustrate the implementation of specific operations.

2.1 install virtualenv

To create a virtual environment, you need to first install a module called virtualenv. First make sure you have pip installed on your computer.

# If pip is not installed on the computer, you need to use the following command to install pip first, different operating system commands will be different: 
sudo apt-get install python-pip 

# Then install virtualenv through pip, you can check the version of virtualenv after installation: 
pip install virtualenv 
virtualenv --version

2.2 Create a virtual environment and activate it

After successfully installing virtualenv, you can create a virtual environment with a specific name using the following command:

# Create a virtual environment with a specific name, myvenv can be replaced with any name you like 
virtualenv myvenv 
# Executing the above command will create a folder of myvenv, including all executable files needed to run the Python project 

# If you want to specify the Python version, You can create a virtual environment with the following command: 
python2.7 virtualenv myvenv # Create a virtual environment using Python2.7 
# You can also use the following command to create a virtual environment, the effect is the same as above 
python3 -m venv myvenv # Create using Python3 The virtual environment 

# After creating the virtual environment, you need to use the following command to activate and enter the specific isolation environment 
source venv/bin/activate 
# If it is under the Windows system, the activation command above is slightly different: 
venv/Scripts/ activate

After successfully activating and entering the virtual environment, as shown in the figure below, you will see the sign (.venv) in front of the command line:

Once the virtual environment has been successfully created and activated, the dependencies and requirements required by the project can be installed without interfering with other projects.

2.3 Exit the virtual environment

Once you have finished working on the current project, you can return to the system default environment, just use the deactivate command to exit.

How the three virtual environments work

If the virtual environment is not used, the third-party packages of Python will be installed by pip to the site-packages directory of the Python installation path. When using a virtual environment, after creating a virtual environment according to the previous method of "python3 -m venv myvenv", you can view the current directory and see several files:

  • Bin directory: executable files such as python3 and pip3 are actually soft links to the Python system directory (or a copied copy)
  • lib/python3.10/site-packages: used for the storage directory of the three-party packages installed in the virtual environment

Therefore, through venv, the system's Python will be soft-linked to the venv environment. When the source command is used to activate the virtual environment, the python and pip under the venv will be enabled. At the same time, the three-party packages installed in the process are placed in the venv/lib/pythonx.x/site-packages, so that the system's Python environment will not be affected in any way.

Four more usages of virtual environments

4.1 Three-party package dependency generation and reinstallation

As mentioned before, after creating a new virtual environment, the pip and setuptools packages are symlinked (or copied) into the virtual environment, and any other packages used in the environment can then be installed. For projects with complex requirements, it is best to keep a requirements.txt file in the root directory of the project, which lists the requirements of the project's three-party package dependencies. This way, if you need to recreate the virtual environment, you can use the pip install -r requirements.txt command to reinstall all required packages.

After installing the required libraries in the project's virtual environment, you can use pip list to view all currently installed libraries, and then you can generate a text file listing all project dependencies by running the following command:

pip freeze > requirements.txt

Then, when rebuilding the environment of this project on other computers, you can quickly install all the dependent three-party packages at once through the following command:

pip install -r requirements.txt

4.2 Virtual environment update and upgrade

The copies of pip and setuptools in a virtual environment are local to that virtual environment and need to be updated and maintained independently. So you will encounter warnings that pip is out of date in some virtual environments, but not in other virtual environments. pip must be updated separately in each virtual environment.

Therefore, when upgrading the Python runtime on the system, the virtual environment using this version of Python will not be automatically upgraded. This design is also reasonable, because inadvertently upgrading to a new Python version may break their third-party package dependencies.

The specific virtual environment update and upgrade operations are as follows:

Python minor version upgrade:

If you have upgraded an existing small version of Python, such as from Python 3.9.5 to Python 3.9.7, you can upgrade the virtual environment in the following way in the project directory (do not activate the virtual environment in advance, otherwise the upgrade may fail):

python -m venv myvenv --upgrade

Python major version upgrade:

If you have already installed a major new version of Python, such as Python 3.8 and will now install Python 3.9, then do not use the above upgrade method, but recreate a new virtual environment to use the new Python big version.

How to configure venv in Visual Studio Code

In addition to the use of virtualenv introduced earlier, conda also has the function of virtual environment. conda will be installed in Anaconda and Miniconda, and should be used for projects used to develop data science.

In VS Code, if the virtual environment is created through the command line and under the project directory, VS Code can be automatically detected. If it is not created through the previous command line, it can also be created through the following method. In VS Code, use "Command+Shift+P" to call up the interactive command, and enter Python: env..., as shown in the figure below:

Then you can choose the tool to create a virtual environment, the options are Venv and Conda:

Then select the Python version for that virtual environment:

Next, wait for a short time and wait for the virtual environment in VS Code to be automatically created.

Finally, you can write a test program, as shown below, by clicking the triangle button on the right, you can run or debug the .py code:

references:

https://blog.loginradius.com/engineering/python-virtual-environments/

Virtualenv and venv: Python virtual environments explained | InfoWorld

How to Set Up a Virtual Environment in Python – And Why It's Useful

venv — Creation of virtual environments — Python 3.11.0 documentation

Guess you like

Origin blog.csdn.net/suxiang198/article/details/127992039