Learn about virtual environments
If you want to develop multiple different projects on one machine, you need to use different versions of the same package. If you also use the above command to install or update in the same directory, other projects must not be able to run. How to do it?
Solution: Virtual Environment
So what is a virtual environment?
The virtual environment is actually a copy of the real python environment, so that we install packages in the copied python environment without affecting the real python environment. By establishing multiple virtual environments, developing projects in different virtual environments achieves isolation between projects.
Installation of virtual environment library
We use virtualenv here to create a virtual environment.
pip's default source installation of the third library will be a bit slow, you can configure other mirror sources.
Install on Windows
existcmdwindow to see if the system is installed
pip install virtualenv
pip3 install virtualenv pip3 install virtualenvwrapper-win
virtualenvwrapper-winVirtual environment extension package (convenient instruction)
virtual environment creation
existcmdCreate a python3 virtual environment under the window
mkvirtualenv -p python3 py_django
The virtual environment created by Windows defaults to
C:\Users\Administrator\Envs, Administrator represents the user my current computer is using, yours may be under other users, if you need to specify the storage path of the created virtual environment, you can add it in the system environment variable of the computer
Install on Linux
Install a virtual environment
sudo pip3 install virtualenv sudo pip3 install virtualenvwrapper
After installing the virtual environment, enter the following command to test
If the prompt cannot be found
mkvirtualenvcommand, environment variables must be configured
# 1. Create a directory to store the virtual environment mkdir $HOME/.virtualenvs # 2. Open ~/.bashrc file vim ~/.bashrc # 2.1 Add the following two lines export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh # 3. Run source ~/.bashrc
WORKON_HOMEThe location where the virtual environment directory is stored can be set independently.
Create a virtual environment
Enter the virtual environment
Exit the virtual environment
If the environment is a real environment, it will prompt
deactivate: command not found
virtual environment directives
The instructions are the same on both Windows and Linux platforms.
Exit first: deactivate Then delete: rmvirtualenv virtual environment name
Virtual Environment Summary
1. Using a virtual environment will not mess up many libraries of the main interpreter
2. For example, if you install Flask in a separate virtual environment, you will know all the libraries used in subsequent development. Use
pip freezeThe command knows what environment your project needs, and there are many libraries that are not used by the project in the main interpreter. Using a virtual environment to deploy to the server is convenient for environment configuration, and there is no need to filter the required libraries one by one.
Use in a virtual environment
pip freezeCheck the library environment required by the project.
pip freeze > requirements.txtLibrary information required for exporting the project.
pip install -r requirements.txBulk install libraries required by the project.
3. Develop multiple projects on the same machine, but encounter inconsistent versions of the same library required by the project. Therefore, the virtual environment is used to isolate each project independently, and different version libraries can be used separately.
So far, this article about how the Python virtual environment virtualenv is used is introduced here. For more related Python virtual environment virtualenv content, please search for previous articles of developpaer or continue to browse the related articles below. I hope you will support developpaer more in the future!