1. What is a virtual environment?
The meaning of a virtual environment is just like a virtual machine. It can realize that Python dependent packages in different environments are independent of each other without interference.
Give an example.
Suppose there are two projects in our computer, and they both use the same third-party package, everything goes well. But for some reason, Project B needs to use some new features of this third-party package for some reasons (the new version only has it), and if it is rashly upgraded like this, we cannot evaluate the impact on Project A. At this time, we will There is a special need for a solution that allows projects A and B to be in two different Python environments. Do not affect each other.
In order to facilitate everyone's understanding of the virtual environment, I listed the following advantages:
- Make different application development environments independent
- Environment upgrade does not affect other applications, nor does it affect the global python environment
- Can prevent package management confusion and version conflicts in the system
There are many tools for managing Python version and environment on the market, here are a few:
p
: Very simple interactive python version management tool.pyenv
: Simple Python version management tool.Vex
: You can execute commands in a virtual environment.virtualenv
: A tool for creating an independent Python environment.virtualenvwrapper
: A set of extensions to virtualenv.
There are many tools, but I personally think that the best one is virtualenvwrapper
, and I recommend everyone to use it.
2. virtualenv
Since virtualenvwrapper is a set of extensions of virtualenv, if you want to use virtualenvwrapper, you must first install virtualenv.
installation
$ pip install virtualenv
# 检查版本
$ virtualenv --version
Basic use
Because virtualenv creates a virtual environment in the current environment. So we have to prepare a directory dedicated to storing the virtual environment. (The following operations are being completed in Linux, windows are relatively simple, please complete by yourself, if you don’t understand, please contact me on WeChat.)
create
# 准备目录并进入
$ mkdir -p /home/wangbm/Envs
$ cd !$
# 创建虚拟环境(按默认的Python版本)
# 执行完,当前目录下会有一个my_env01的目录
$ virtualenv my_env01
# 你也可以指定版本
$ virtualenv -p /usr/bin/python2.7 my_env01
$ virtualenv -p /usr/bin/python3.6 my_env02
# 你肯定觉得每次都要指定版本,相当麻烦吧?
# 在Linux下,你可以把这个选项写进入环境变量中
$ echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7" >> ~/.bashrc
Enter/exit
$ cd /home/wangbm/Envs
# 进入
$ source my_env01/bin/activate
# 退出
$ deactivate
Delete To delete a virtual environment, just delete the corresponding folder. It will not affect the global Python and other environments.
$ cd /home/wangbm/Envs
$ rm -rf my_env01
Note: The created virtual environment will not contain third-party packages of the native global environment, which will ensure the cleanliness of the newly created virtual environment.
If you need to use the same third-party package as the global environment. The following methods can be used:
# 导出依赖包
$ pip freeze > requirements.txt
# 安装依赖包
$ pip install -r requirements.txt
3. virtualenvwrapper
Although virtualenv is quite easy to use, its functions are still not perfect.
You may have also discovered that to enter the virtual environment, you must keep in mind the virtual environment directory set before. If you follow the rules every time, it will be fine to install the environment in a fixed directory. But in many cases, people are lazy. There may be many virtual environments scattered around the system, and you may forget their names or locations.
One more thing, virtualenv requires two steps to switch the environment, exit -> enter. Not easy enough.
In order to solve these two problems, virtualenvwrapper was born.
installation
# 安装 - Linux
pip install virtualenvwrapper
# 安装 - Windows
pip install virtualenvwrapper-win
Configuration First find virtualenvwrapper.sh
the location of the file
find / -name virtualenvwrapper.sh
# /usr/bin/virtualenvwrapper.sh
If it is windows, use everything to find the virtualenvwrapper.bat script
D:\Program Files (x86)\Python38-32\Scripts\virtualenvwrapper.bat
Add configuration in ~/.bashrc file
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace
export VIRTUALENVWRAPPER_SCRIPT=/usr/bin/virtualenvwrapper.sh
source /usr/bin/virtualenvwrapper.sh
If it is windows, add environment variables:WORKON_HOME
Basic syntax :
mkvirtualenv [-a project_path] [-i package] [-r requirements_file] [virtualenv options] ENVNAME
Common method
# 创建
$ mkvirtualenv my_env01
# 进入
$ workon my_env01
# 退出
$ deactivate
# 列出所有的虚拟环境,两种方法
$ workon
$ lsvirtualenv
# 在虚拟环境内直接切换到其他环境
$ workon my_env02
# 删除虚拟环境
$ rmvirtualenv my_env01
Other commands
# 列出帮助文档
$ virtualenvwrapper
# 拷贝虚拟环境
$ cpvirtualenv ENVNAME [TARGETENVNAME]
# 在所有的虚拟环境上执行命令
$ allvirtualenv pip install -U pip
# 删除当前环境的所有第三方包
$ wipeenv
# 进入到当前虚拟环境的目录
$ cdsitepackages
# 进入到当前虚拟环境的site-packages目录
$ cdvirtualenv
# 显示 site-packages 目录中的内容
$ lssitepackages
For more information, please check the official document https://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html
4. Practical demonstration
The above content is a user guide. Next, let's take a look at how to use the virtual environment in the project.
How to use our virtual environment in our development
Usually we use the following scenarios
- Interactive
- PyCharm
- In project
Next, I will show them one by one.
4.1 Interactive
First, compare the difference between the global environment and the virtual environment. There are requests package in the global environment but not installed in the virtual environment. When we knock workon my_env01
, in front of my_env01
the logo, that we are already in a virtual environment. All subsequent operations will be performed in a virtual environment.
4.2 In the project
Our engineering project has an entry file. Observe carefully, the Python interpreter can be specified in the first line.
If we want to run this project in a virtual environment, just change the file header.
Now I still take, import requests
as an example, to illustrate whether it is running in a virtual environment, if it is, the same as above, an error will be reported.
document content:
#!/root/.virtualenvs/my_env01/bin/python
import requests
print "ok"
Before running, pay attention to adding execution permissions.
$ chmod +x ming.py
All right. Let's execute
$ ./ming.py
It was found that it was as expected, and an error was really reported. It shows that the virtual environment we specified is effective.
4.3 PyCharm
Click File-Settings-Project-Interpreter and click the small gear. Click to add as shown in the figure, and follow the prompts to add a virtual environment. Then click OK to use this virtual environment, and all subsequent projects will run in this virtual environment.