What is a virtual environment
Installing python is equivalent to installing a global environment.However, when the application is developed and used, I want to have an isolation to avoid the influence of other applications.So, Python uses virtual environments.The virtual environment is equivalent to a copy of the Python native environment. The standard library used is the same, and each copy will cause a lot of redundancy.Therefore, the Python virtual environment directly calls the native standard library.
Use of virtual environment
Create a virtual environment
python(python3) -m venv [name of virtual environment] Create a virtual environment in the current directoryEnvironment
Enter the virtual environment
cd [Virtual environment name directory]/bin Enter the virtual environment directory in the current directory source activate Activate the current virtual environment
Exit the virtual environment
deactivate Exit the current virtual environment
Importance of the virtual environment
- IfTo do a lot of tasks in an environment, you need to install many third-party packages. Sometimes there will be version incompatibility between third-party packages and packages, resulting in unavailability; and it is difficult to manage an environment when there are too many packages.If we restrict each task to operate in a virtual environment and install only the packages required by this task, the probability of incompatibility problems will be greatly reduced.
- You can configure the running environment of other people's projects directly through the virtual environment, so that you can run other people's code.
Save the virtual environment
During the development of Python projects, third-party libraries are usually used.It can be executed directly in the virtual environment: pip freeze > requirements.txt Freeze the library of the virtual environment and save it to the requirements.txt file.In this way, the application environment can be built at any time anywhere.