Detailed explanation of python's virtual environment graphics (virtual environment function and virtual environment construction, switching, exit, migration and packaging) code demonstration

Table of contents

1. Virtual environment

1.1. Conceptual understanding

1.2. Function 

2. Virtual environment operation tutorial

2.1. Use pycharm to build, switch, and delete the virtual environment

2.1.1. Build a virtual environment

2.1.2. Switch virtual environment

​edit

2.1.3. Delete the virtual environment

2.2. Build by command line (mac/linux/unix)

2.2.1. Build a virtual environment

2.2.2. Switch virtual environment

2.2.3. Delete the virtual environment

2.4. Virtual environment migration and packaging

2.4.1 Packaging

2.4.2 Migration


1. Virtual environment

1.1. Conceptual understanding

       Venv: virtual environment, which is a module used by Python to create and manage virtual environments. This module is for you to store your Python scripts and install various third-party Python modules. The environment in the module is completely separate from the local machine. That is to say, the Python third-party modules you install through pip under venv will not exist in your local environment.

      Like the above, you may not understand the virtual environment very well, and then look at the following functions, you can probably understand it

1.2. Function 

       Python's virtual environment allows a Python program to have an independent library library and interpreter interpreter, instead of sharing a unified library and interpreter with other Python programs. The advantage of the virtual environment is to avoid the mutual influence between different Python programs (using the global library and interpreter together)

      For example: program A needs version 2.0 of a certain library, and program B needs version 3.0 of the same library. If there is no virtual environment, we can only have one version of this library locally. If program B is executed, it means locally installed 3.0 version of the library, A cannot be executed successfully.

2. Virtual environment operation tutorial

       Using pycharm to do a little bit may be more friendly than the command line download method, but it is recommended to also learn the command line method. If you need to log in to the server, you may only use the command line to operate.

2.1. Use pycharm to build, switch, and delete the virtual environment

2.1.1. Build a virtual environment

1) Create a project, open pycharm, click new project

2) And create a virtual environment new environment for this project, select the python version you want to use in the base iterpreter, and click create

3) Open the following terminal, if the brackets in front are displayed, it proves that the virtual environment is activated successfully

 4) Enter the settings sync in flie, the path is as shown in the figure below,

5) You can click on the red arrow here to download some packages, and the blue arrow can change the python version

2.1.2. Switch virtual environment

1) Enter the settings sync in flie, the path is as shown in the figure below,

2) Pull down to select other virtual environments, click apply in the lower right corner and then ok.

 If there is no other virtual environment in the drop-down list, you can perform the following operations, and select a virtual environment in the file through the add interpreter on the right.

3) Click apply in the lower right corner and then ok, the venv has been switched successfully. 

2.1.3. Delete the virtual environment

Go directly to delete the files in the created virtual environment.

2.2. Build by command line (mac/linux/unix)

2.2.1. Build a virtual environment

1) Open the terminal and install virtualenv:

instruction:

pip install virtualenv

Demo:

 2) Create a virtual environment where you want it: venv_demo is the name of the newly created virtual environment. At the same time, a folder venv_demo with the same name as the virtual environment will be created, which stores an independent Python execution environment.

Instruction: venv_demo is just the name of my virtual environment for demonstration, which can be replaced freely

virtualenv venv_demo

Demo: ll command view, venv_demo has been successfully created

2) To enter the virtual environment, you need to activate the command

Order:

source ven_demo/bin/activate

Demo:

After entering the virtual environment, the prompt of the command line will add the name of the virtual environment (that is, add a bracket before it, and the inside of the bracket is the environment name)

 3) So far, successfully created

2.2.2. Switch virtual environment

1) First, exit the virtual environment

Order:

deactivate

Demo:

2) Exit first, then activate other virtual environments

2.2.3. Delete the virtual environment

Order:

rm -r venv_demo

Demo:

You can use the ll command to check to see if the deletion is successful. Here is a demonstration that I deleted it without exiting the virtual environment first. It is recommended to exit the virtual environment (deactivate) first, and then delete the virtual environment

2.4. Virtual environment migration and packaging

2.4.1 Packaging

Import the installation package version information into the requirements.txt file, pay attention to the --all parameter, adding this parameter will package the setuptools and urllib3 packages; if not added, these two will not be packaged

pip freeze --all > requirements.txt

2.4.2 Migration

In the requirements.txt file directory, you need to migrate to the virtual environment terminal command and execute:

pip install -r requirements.txt #下载安装依赖包

Guess you like

Origin blog.csdn.net/weixin_45440484/article/details/130144943