Python virtual environment: virtualenv, pythonvirtualenv
Virtualenv
Virtualenv is used to create an independent Python environment. Multiple Python environments are independent of each other and do not affect each other. It can:
1. Install the new suite without permission
2. Different Application versions can be used.
3. Kit upgrade does not affect other applications
Install
sudo apt-get install python-virtualenv
Usage
Virtualenv [Virtual Environment name]
For example, create a ** ENV ** virtual environment.
virtualenv ENV
By default, the virtual environment depends on the site packages in the system environment, that is, the third-party packages installed in the system will also be installed in the virtual environment. If you do not want to rely on these packages, you can add the Parameter--no-site-packages
Create a virtual environment
Virtualenv -- no-site-packages [Virtual Environment name]
Start the Virtual Environment
cd ENVsource ./bin/activate
Note that at this time, the command line will have one more(ENV)
, ENV is the name of the virtual environment. Then, all modules will only be installed in this directory.
Exit the Virtual Environment
deactivate
Install Python kit in a Virtual Environment
Virtualenv comes with the pip Installation tool, so you can directly run the installation kit:
Pip install [suite name]
If the virtual environment is not started and the pip tool is installed in the system, the kit will be installed in the system environment. To avoid this problem, you can~/.bashrc
Add the following to the file:
export PIP_REQUIRE_VIRTUALENV=true
Or enable the system to automatically enable the virtual environment when executing pip:
export PIP_RESPECT_VIRTUALENV=true
Virtualenvwrapper
Virtaulenvwrapper is the extension package of virtualenv, which is used to facilitate the management of virtual environments. It can do the following:
1. Integrate all virtual environments into one directory
2. Manage (add, delete, and copy) Virtual Environment
3. Switch the Virtual Environment
4 ....
Install
sudo easy_install virtualenvwrapper
Virtualenvwrapper cannot be used yet. By default, virtualenvwrapper is installed under/usr/local/bin. In fact, you need to run virtualenvwrapper. sh file. Don't worry. Open this file and check it. There are installation steps in it. We can set the environment according to the operation.
Now virtualenvwrapper can be used.
List Virtual Environments
workon
You can also use
lsvirtualenv
Create a virtual environment
Mkvirtualenv [Virtual Environment name]
Start/switch Virtual Environment
Workon [Virtual Environment name]
Delete Virtual Environment
Rmvirtualenv [Virtual Environment name]
Leave the Virtual Environment
deactivate