Python Virtual Environment: virtualenv
Blog Category:
Pythonavoid conflicts between different versions of Python or Python versions of components during Python development,
It is necessary to configure Python's virtual environment, this blog is transferred from http://liuzhijun.iteye.com/blog/1872241
Virtualenv
Virtualenv is used to create a stand-alone Python environment where multiple python is independent from each other and can:
1. Install the new kit without permission
2. Different applications can use different versions of the kit
3. Package upgrade does not affect other applications
Installation
sudo apt-get install python-virtualenv
How to use
For example, create a **env** virtual environment
virtualenv ENV
By default, the virtual environment relies on site packages in the system environment, which means that the installed third-party package in the system will also be installed in the virtual environment, if you do not want to rely on the package, you can add parameters to --no-site-packages
establish a virtual environment
virtualenv --no-site-packages [虚拟环境名称]
Start the virtual environment
cd ENVsource ./bin/activate
Note that at this point the command line one more (ENV)
, env is the virtual environment name, then all the modules will only be installed in the directory.
Exiting a virtual environment
deactivate
Installing a Python suite in a virtual environment
The virtualenv comes with the PIP installation tool, so the kit that needs to be installed can be run directly:
pip install [套件名称]
If the virtual environment is not started and the PIP tool is installed, the package will be installed in the system environment, in order to avoid this, you can ~/.bashrc
add the following file:
export PIP_REQUIRE_VIRTUALENV=true
or let the system automatically turn on the virtual environment when the PIP is executed:
export PIP_RESPECT_VIRTUALENV=true
Virtualenvwrapper
Virtaulenvwrapper is an expansion pack for virtualenv that makes it easier to manage virtual environments and can do:
1. Integrate all virtual environments into one directory
2. Manage (add, delete, copy) virtual environments
3. Switch the virtual environment
4 .....
Installation
sudo easy_install virtualenvwrapper
At this time can not use Virtualenvwrapper, the default virtualenvwrapper installed under/usr/local/bin, in fact, you need to run the virtualenvwrapper.sh file before the line, do not worry, open this file to see, There are installation steps, we set the environment according to the operation.
Create a directory to hold a virtual environment
mkdir $HOME/.virtualenvs
Add rows in ~/.BASHRC: export workon_home= $HOME/.virtualenvs
Add rows in ~/.BASHRC: source/usr/local/bin/virtualenvwrapper.sh
Run:source ~/.bashrc
At this point the Virtualenvwrapper is ready to use.
List Virtual Environments
workon
You can also use
lsvirtualenv
Create a new virtual environment
mkvirtualenv [虚拟环境名称]
Start/Switch Virtual environments
workon [虚拟环境名称]
Deleting a virtual environment
rmvirtualenv [虚拟环境名称]
Leave the virtual environment
deactivate
Reference:
http://www.virtualenv.org/en/latest/
Http://stackoverflow.com/questions/11372221/virtualenvwrapper-not-found
http://www.openfoundry.org/tw/tech-column/8516- Pythons-virtual-environment-and-multi-version-programming-tools-virtualenv-and-pythonbrew
Http://virtualenvwrapper.readthedocs.org/en/latest/index.html
Python's Virtual Run environment