In Python
the process of using development, if there are more than one project, it is inevitable that different projects will depend on different versions of libraries; or during the development process, the physical environment will not be filled with various libraries, causing future dependence disaster. At this time, we need to use different virtual environments for different projects to keep the development environment and the host environment clean.
virtualenv是
One can help us manage different Python
environments, and can create multiple different virtual environments in the system that do not interfere with each other.
1. Install and configure virtualenv
1.1 download package
Specify Tsinghua source to download pip package
root@darren-virtual-machine:~# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple virtualenv
Upgrade pip tool
root@darren-virtual-machine:~# pip3 install --upgrade pip
1.2 Install virtualenv
root@darren-virtual-machine:~# pip3 install virtualenv
1.3 Create an independent virtual environment
root@darren-virtual-machine:~/PycharmProjects/drfdemo# virtualenv --python=python3 venv
created virtual environment CPython3.6.9.final.0-64 in 371ms
creator CPython3Posix(dest=/root/PycharmProjects/drfdemo/venv, clear=False, global=False)
seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1)
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
root@darren-virtual-machine:~/PycharmProjects/drfdemo# ll
drwxr-xr-x 4 root root 4096 4月 12 10:28 venv/
1.4 Simple operation of virtual environment
Enter the virtual environment
root@darren-virtual-machine:~/PycharmProjects/drfdemo# source venv/bin/activate
Use a virtual environment to install third-party packages
(venv) root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 install django==1.9.8
View
(venv) root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 list
Package Version ---------- ------- Django 1.9.8 pip 20.0.2 setuptools 46.1.3 wheel 0.34.2
Exit the virtual environment
(venv) root@darren-virtual-machine:~/PycharmProjects/drfdemo# deactivate
Install virtual environment virtualenvwrapper
virtualenv
One of the biggest disadvantages is:
Go to the virtual directory environment where open virtual environment before each bin
directory source
look activate
, which we need to remember where the directory for each virtual environment.
And it is also possible that you forget where the virtual environment is placed. . .
- A feasible solution is to centralize all the virtual environment directories, such as / opt / all_venv /, and do different things for different directories.
- Use virtualenvwrapper to manage your virtual environment (virtualenv), in fact, he is a unified management of the virtual environment directory, and save the source step.
Two virtual environment virtualenvwrapper
2.1 Install virtual environment virtualenvwrapper
root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 install virtualenvwrapper
Set up Linux user personal profile
WORKON_HOME = ~ Export / Envs # Set virtualenv unified management directory, after the automatic download of the virtual environment, all of which are on the Export VIRTUALENVWRAPPER_VIRTUALENV_ARGS = '' # add parameters virtualenvwrapper generate clean and isolated environment export VIRTUALENVWRAPPER_PYTHON = / usr / bin / python3
Execute the virtualenvwrapper installation script
(drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# source /usr/local/bin/virtualenvwrapper.sh
(drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# source /root/.bashrc
2.2 Create a virtual environment for easy use
root@darren-virtual-machine:~/PycharmProjects/drfdemo# mkvirtualenv drfdemo -p python3
created virtual environment CPython3.6.9.final.0-64 in 325ms creator CPython3Posix(dest=/root/Envs/drfdemo, clear=False, global=False) seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1) activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator virtualenvwrapper.user_scripts creating /root/Envs/drfdemo/bin/predeactivate virtualenvwrapper.user_scripts creating /root/Envs/drfdemo/bin/postdeactivate virtualenvwrapper.user_scripts creating /root/Envs/drfdemo/bin/preactivate virtualenvwrapper.user_scripts creating /root/Envs/drfdemo/bin/postactivate virtualenvwrapper.user_scripts creating /root/Envs/drfdemo/bin/get_env_details
(drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 list
Package Version ---------- ------- pip 20.0.2 setuptools 46.1.3 wheel 0.34.2
Install a third-party plugin
(drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 install django
(drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 install django Collecting django Using cached Django-3.0.5-py3-none-any.whl (7.5 MB) Collecting asgiref~=3.2 Using cached asgiref-3.2.7-py2.py3-none-any.whl (19 kB) Collecting pytz Using cached pytz-2019.3-py2.py3-none-any.whl (509 kB) Collecting sqlparse>=0.2.2 Using cached sqlparse-0.3.1-py2.py3-none-any.whl (40 kB) Installing collected packages: asgiref, pytz, sqlparse, django Successfully installed asgiref-3.2.7 django-3.0.5 pytz-2019.3 sqlparse-0.3.1
(drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# pip3 list
Package Version ---------- ------- asgiref 3.2.7 Django 3.0.5 pip 20.0.2 pytz 2019.3 setuptools 46.1.3 sqlparse 0.3.1 wheel 0.34.2
Exit the virtual environment
(drfdemo) root@darren-virtual-machine:~/PycharmProjects/drfdemo# deactivate
Delete a virtual environment
root@darren-virtual-machine:~/PycharmProjects/drfdemo# rmvirtualenv drfdemo
Removing drfdemo...
Reference: https://www.cnblogs.com/tiger666/articles/10312546.html