Python3 virtual environment settings

In daily development, if you can flexibly use the Python virtual environment (virtual environment), you can solve many annoying problems. Using the Python virtual environment allows you to use different versions of libraries on the same computer, and you can switch easily. For example, our current stable running version is Django 2.x. If we want to update it to Django 3.x, we can use a virtual environment. We can install different versions of Django in different virtual environments, so that we can switch to different versions for development and testing at any time.

1. The creation of
a virtual environment Steps to create a virtual environment:

1. The command to create a virtual environment [.venv-lab] in Python 3 is:

E:\pythonenv>python -m venv .venv-lab

1.1. After the virtual environment is created, a folder (.venv-lab) will be generated in the current directory. We usually don’t need to change any files in this folder, so we usually set it as a hidden folder

2. Activation environment and use environment [activate.bat]

.venv-lab\Scripts\activate.bat

3. Display after activation

(.venv-lab) E:\pythonenv\.venv-lab\Scripts>  

4. Direct installation environment

Demonstrate the installation of numpy for testing

pip install numpy
(.venv-lab) E:\pythonenv\.venv-lab\Scripts>pip install numpy

5. The command "pip freeze" to check which software packages are installed in the virtual environment

(.venv-lab) E:\pythonenv\.venv-lab\Scripts>pip freeze
numpy==1.19.5

(.venv-lab) E:\pythonenv\.venv-lab\Scripts>  

It can be seen from this that currently only a numpy version is installed, which is 1.19.5

2. Porting and Sharing of Virtual Environment

A major advantage of using a virtual environment is that it is easy to transplant and share. After creating a virtual environment, we can export the settings of the virtual environment into a file, and then generate the exact same environment on another machine.

2.1 The command to export the environment is:

“pip freeze > requirements.txt”

(.venv-lab) E:\pythonenv\.venv-lab\Scripts>pip freeze >requirements.txt

The "requirements.txt" file is generated in the current folder

2.2 Import environment commands:

In your new environment, run the following command

pip install -r requirements.txt

2.3 Run the command to verify that the environment is consistent

pip freeze

2.4 Ways to stop using the virtual environment

When an environment is activated, the impact of the environment is global, which means that the commands in the environment can be run anywhere, not limited to the folder where it is located. When you do not want to use the virtual environment, you can use the deactivate command to deactivate the environment. If you don't want to use a certain environment, you can delete the folder where the environment is located. But it should be noted that before deleting the environment folder, be sure to deactivate the environment, otherwise some commands will not work properly. At this time, you will need to reset the environment variables. The simple way to deal with it is to turn off the terminal and reopen it.

Note: The environment deactivate here is under the ".venv-lab\Scripts" folder, that is, under the same folder as the activation above

The windows virtual environment does not explain the installed software, you can find the installed content information on Baidu by yourself.

Guess you like

Origin blog.csdn.net/wtt234/article/details/113369557