Creating Python virtual environment virtualenv and virtualenvwrapper
1. Why do we need to build a virtual environment
When two projects on the machine depend on different versions of the same package, the project will fail to run. At this time, you can install the virtual environment.
2. What is virtual environment
A virtual environment is a copy of the python environment
A network connection is required to create a virtual environment
3. Install python, PIP and virtualenv
ubuntu: Sudo apt get install python3 # install python3 Sudo apt get install Python pip Sudo PIP install virtualenv # install virtual environment Sudo PIP install virtualenvwrapper centos: Sudo Yum - y install python36 # install python3 Sudo Yum - y install python36 PIP # install pip Yum install Python virtualenv # install virtual environment Yum install Python virtualenvwrapper # install virtual environment expansion pack
Check the installation:
python3.6 -V pip3.6 -V
Add soft link:
#Using Python 3 to use Python 3.6: ln -s /usr/bin/python3.6 /usr/bin/python3 #Copy code pip3.6 ln -s /usr/bin/pip3.6 /usr/bin/pip3
4. Edit the virtual file and run it
#Edit files in home directory vim .bashrc #Add the virtual environment address at the end (virtual files are stored in. Virtualenvs) export WORKON_HOME=$HOME/.virtualenvs #Add commands that can be used directly in the virtual environment source /usr/bin/virtualenvwrapper.sh #Run the file and take effect source .bashrc
5. Create a virtual environment
#Create a python 3 virtual environment and name it mkvirtualenv -p python3 milepost #Exit virtual environment deactivate #Working in a virtual environment workon web_working #List all virtual environments lsvirtualenv -b #Cpvirtualenv replication environment Cpvirtualenv env1 env3 (copy env1 to env3) #List the content of site packages in the current environment (executed in the environment) lssitepackages #Clear all third party packages in the environment cdsitepackages #Delete virtual environment project Rmvirtualenv + virtual environment name
1. The python used in the virtual environment is the copied python, and the installation of the python package is also installed in the copied python. Any operation such as creating files in the virtual environment exists in the real environment, just copying Python and installing the package of python, which is different from the real environment.
2. You can’t use sudo PIP install to install the package in the virtual environment. This command will install the package in the real environment, so sudo should be removed
The above is Xiaobian’s introduction to the installation of Python virtual environment under linux environment and the precautions. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply you in time. Thank you very much for your support to developer!
If you think this article is helpful to you, please reprint, please indicate the source, thank you!