1. What is a virtual environment ？
The meaning of virtual environment , Just as virtual machine equally , It can be implemented in different environments Python Dependent packages are independent of each other , Mutual interference .
Let's give you an example .
Suppose we have two projects in our computer , They all use the same third-party package , Everything was going well . But for some reason , project B Some of the new features of this third-party package have to be used for some reason （ The new edition is available ）, And if it just escapes , For the project A We cannot assess the impact of , At this time, we especially need a solution to make the project A and B, In two different Python Environment . They don't influence each other .
In order to facilitate you to have an understanding of the virtual environment , I've listed the advantages ：
- Make different application development environment independent
- Environment upgrade does not affect other applications , And it won't affect the overall situation python Environmental Science
- It can prevent package management confusion and version conflict in the system
Management in the market Python There are many tools for versions and environments , Here are a few ：
p： Very simple interactive python Version management tools .
pyenv： ordinary Python Version management tools .
Vex： You can execute commands in a virtual environment .
virtualenv： Create independence Python Tools for the environment .
virtualenvwrapper：virtualenv A set of extensions .
A lot of tools , But I think the best to use , when
virtualenvwrapper, I recommend that you also use .
because virtualenvwrapper yes virtualenv A set of extensions , So if you want to use virtualenvwrapper, It must be installed first virtualenv.
install Basic use *
because virtualenv Creating a virtual environment is created in the current environment . So we need to prepare a special directory to store the virtual environment .（ The following operations are Linux At the completion of ,windows Relatively simple , Please complete by yourself , If you don't understand, please contact me on wechat .）
# Prepare the catalog and go to $ mkdir -p /home/wangbm/Envs $ cd !$ # Creating a virtual environment （ By default Python edition ） # After execution , There will be one in the current directory my_env01 The catalog of $ virtualenv my_env01 # You can also specify the version $ virtualenv -p /usr/bin/python2.7 my_env01 $ virtualenv -p /usr/bin/python3.6 my_env02 # You must think that you have to specify the version every time , It's a lot of trouble ？ # stay Linux Next , You can write this option into the environment variable $ echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7" >> ~/.bashrc
Get into / sign out
$ cd /home/wangbm/Envs # Get into $ source my_env01/bin/activate # sign out $ deactivate
Delete virtual environment , Just delete the corresponding folder . It won't affect the overall situation Python And other environments .
$ cd /home/wangbm/Envs $ rm -rf my_env01
Be careful ：
Create a virtual environment , Third party packages that do not contain native global environments , It will keep the new virtual environment clean .
If you need to use the same third-party package as the global environment . You can use the following methods ：
# Export dependency package $ pip freeze > requirements.txt # Install dependency packages $ pip install -r requirements.txt
virtualenv Although it's quite easy to use , But the function is not perfect .
You may have found out , To get into a virtual environment , You have to keep in mind the virtual environment directory you set up before , If you follow the rules every time , It's OK to install the environment in a fixed directory . But a lot of times , People are lazy , There may be many virtual environments scattered around the system , You will probably forget their names or locations .
And a little bit more ,virtualenv Switching environments takes two steps , sign out -> Get into . Not easy enough .
To solve these two problems ,virtualenvwrapper It was born .
# install - Linux pip install virtualenvwrapper # install - Windows pip install virtualenvwrapper-win
First find once
virtualenvwrapper.sh The location of the file
find / -name virtualenvwrapper.sh # /usr/bin/virtualenvwrapper.sh
if windows Then use everything lookup virtualenvwrapper.bat Script
D:\Program Files (x86)\Python38-32\Scripts\virtualenvwrapper.bat
stay ~/.bashrc File new configuration
export WORKON_HOME=$HOME/.virtualenvs export PROJECT_HOME=$HOME/workspace export VIRTUALENVWRAPPER_SCRIPT=/usr/bin/virtualenvwrapper.sh source /usr/bin/virtualenvwrapper.sh
if windows Add environment variables ：
Basic grammar ：
mkvirtualenv [-a project_path] [-i package] [-r requirements_file] [virtualenv options] ENVNAME
# establish $ mkvirtualenv my_env01 # Get into $ workon my_env01 # sign out $ deactivate # List all virtual environments , The two methods $ workon $ lsvirtualenv # Switch directly to other environments within the virtual environment $ workon my_env02 # Delete virtual environment $ rmvirtualenv my_env01
# List the help documents $ virtualenvwrapper # Copy the virtual environment $ cpvirtualenv ENVNAME [TARGETENVNAME] # Execute commands on all virtual environments $ allvirtualenv pip install -U pip # Delete all third party packages in the current environment $ wipeenv # Go to the directory of the current virtual environment $ cdsitepackages # Into the current virtual environment site-packages Catalog $ cdvirtualenv # Show site-packages Contents of the catalog $ lssitepackages
More , You can see Official documents
4. Actual demonstration
Above contents , It's a guide to use . Next , Let's see , How to use virtual environment in a project .
How to use our virtual environment in our development
Usually we use the following scenarios
- PyCharm in
- In Engineering
Next , I'll show you one by one .
Let's make a comparison , The difference between a global environment and a virtual environment , There are... In the global environment requests package , It's not installed in the virtual environment .
When we type in
workon my_env01, There is
my_env01 The logo of , It shows that we are already in a virtual environment . All the following operations , Will be executed in a virtual environment .
4.2 In the project
Our project , They all have an entry file , Observe carefully , The first line can specify Python Interpreter .
If we're going to run this project in a virtual environment , Just change the file header .
Now I'd like to ,
import requests For example , To illustrate , Whether it is running in a virtual environment , If it is , It's the same as above , Will report a mistake .
The contents of the document ：
#!/root/.virtualenvs/my_env01/bin/python import requests print "ok"
Before running , Pay attention to adding execution permissions .
$ chmod +x ming.py
Okay . Let's do it
The discovery is as expected , It's really wrong . It shows that the virtual environment we specified has effect .
4.3 PyCharm in
Click on File - Settings - Project - Interpreter
Click on the pinion . As shown in the figure, click Add , Follow the prompts to add a virtual environment . Then click OK You can use this virtual environment , Later projects will run in this virtual environment .