After reading the need
9
Minutes
1. What is a virtual environment?
The meaning of virtual environment, like a virtual machine, it can be implemented in different environments, Python dependencies are independent of each other, non-interference. This gives our project a very strong guarantee in a certain degree. Here, I dubbed it "insurance." The whole network is unique.
Let me give you an example.
Let's say we have two items in our computer and they all use the same third party package and everything went well. However, for some reason, project B uses some of the new features of this third-party package for some reason (new versions are available), and if this is so hastily escalated, the impact on project A cannot be evaluated, and this time we need a solution that allows projects A and b to be in two different Python environments. Non-impact.
To facilitate the understanding of the virtual environment, I have 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
Prevents package management clutter and version conflicts from appearing on the system
There are many tools for managing Python versions and environments on the market, here are a few:
P: Very simple interactive Python version management tool.
Pyenv: A simple Python version management tool.
Vex: Commands can be executed in a virtual environment.
Virtualenv: A tool for creating a standalone Python environment.
A set of extensions for virtualenvwrapper:virtualenv.
A lot of tools, but personally think the best use, when belong to Virtualenvwrapper, recommend everyone also use.
2.virtualenv
Because Virtualenvwrapper is a set of extensions for VIRTUALENV, you must first install virtualenv if you want to use Virtualenvwrapper.
Installation
pip install virtualenv# 检查版本virtualenv --version
Because Virtualenv creates a virtual environment, it is created under the current environment. So we're going to prepare a directory that is dedicated to hosting virtual environments. (The following operation in Linux in the completion, Windows relatively simple, please do it yourself, have not understand please QQ contact me.) )
Create
Prepare the directory and make the Mkdir-p/home/wangbm/envs$ CD!$# Create the virtual environment (by default Python version) # Execute, there will be a my_env01 directory under the current directory $ virtualenv my_env01# You can also specify version $ virtualenv-p/usr/bin/python2.7 my_env01$ virtualenv-p/usr/bin/python3.6 my_env02# You must think it's a hassle to specify a version every time? # under Linux, you can write this option into the environment variable $ echo "Export virtualenvwrapper_python=/usr/bin/python2.7" >> ~/.BASHRC
?? Here is still to recommend my own built Python Development Learning Group: 725479218, the group is learning Python development, if you are learning Python, small series welcome you to join, everyone is the software Development Party, Do not regularly share dry goods (only Python software development related), including my own 2018 of the latest Python advanced information and high-level development tutorials, welcome to advanced and into the small partners to deep python
Enter/exit
$ cd /home/wangbm/Envs# 进入$ source my_env01/bin/activate# 退出$ deactivate
Delete the virtual environment , just delete the corresponding folder on the line. Does not affect global Python and other environments.
$ cd /home/wangbm/Envs$ rm -rf my_env01
Note: A virtual environment is created that does not contain a third-party package for the native Global environment, which guarantees a clean new virtual environment.
If you need to use the same third-party package as the global environment. You can use the following methods:
3. Virtualenvwrapper
Although the virtualenv has been quite useful, but the function is not perfect.
You may also find that to get into a virtual environment, you have to keep in mind the virtual environment directory that you set up before, and if you have to install the environment in a fixed directory every time you follow the rules, it's fine. But in many cases, people can be lazy, there may be a lot of virtual environment scattered throughout the system, you will probably forget their name or location.
Also, the VIRTUALENV switch environment takes two steps, exit---enter. Not easy enough.
In order to solve these two problems, Virtualenvwrapper was born.
Installation
# 安装 - Linuxpip install virtualenvwrapper# 安装 - Windowspip install virtualenvwrapper-win
Configuration find the location of the virtualenvwrapper.sh file first
find / -name virtualenvwrapper.sh# /usr/bin/virtualenvwrapper.sh
Added in ~/.BASHRC file
export WORKON_HOME=$HOME/.virtualenvsexport PROJECT_HOME=$HOME/workspaceexport VIRTUALENVWRAPPER_SCRIPT=/usr/bin/virtualenvwrapper.shsource /usr/bin/virtualenvwrapper.sh
Actual Demo
Next, let's take a look at how we use our virtual environment in our development.
We usually use a few of the following scenarios
In interactive
In Pycharm
In engineering
Next, I'll show you.
In interactive
In contrast, the global environment and the virtual environment are different, the global environment has the requests package, and the virtual environment is not installed. When we typed Workon my_env01, we had a MY_ENV01 logo in front of us, indicating that we were already in a virtual environment. All subsequent operations will be performed in a virtual environment.
In the project
All of our projects have an entry file that is carefully observed, and the first line can specify the Python interpreter.
If we are going to run this project in a virtual environment, just change the file header.
Now I still take, import requests as an example, to show whether it is running in a virtual environment, if it is, then the same as above, will error.
File contents:
#!/root/.virtualenvs/my_env01/bin/pythonimport requestsprint "ok"
Before running, be aware of adding execute permissions.
$ chmod +x ming.py
All right. To execute.
$ ./ming.py
The discovery, as expected, really made an error. Indicates that the virtual environment we have specified is effective.
In Pycharm
Click File-settings-project-interpreter
Click on the Pinion. Click Add and follow the prompts to add a virtual environment. Then click OK to use this virtual environment, then the project will be running in this virtual environment.
All right! The above is the full content of this issue.
After you finish this chapter, go ahead and buy a "insurance" for your project.
Development Tools | Buy insurance for your project: Python virtual Environment