Turn from: http://qicheng0211.blog.51cto.com/3958621/1561685 Thank you for the hard work of the author
Virtualenv can build a virtual and stand-alone Python environment, allowing each project environment to be isolated from other projects, keeping the environment clean, and resolving package conflict issues.
First, install the virtualenv
Virtualenv is actually a python package, so we can install it with Easy_install or PIP. The installation methods on the CentOS system are described below.
Easy_install Installation Method:
12 |
[[email protected] ~] # yum install python-setuptools python-devel [[email protected] ~] # easy_install virtualenv |
Pip installation Method:
12 |
[[email protected] ~] # easy_install pip [[email protected] ~] # pip install virtualenv |
Yum installation mode (Epel source):
1 |
[[email protected] ~] # yum install python-virtualenv |
Ii. Creating a Python virtual environment
Create a Python virtual environment using the VIRTUALENV command: virtualenv [virtual Environment name].
123 |
[[email protected] ~] # virtualenv env1 New python executable in env1 /bin/python Installing setuptools, pip... done . |
Once executed, a folder with the same name as the virtual environment is generated locally.
If you have different versions of Python installed in your system, you can use the--python parameter to specify the Python version of the virtual environment:
12345 |
[[email protected] ~]
# virtualenv --python=/usr/local/python-2.7.8/bin/python2.7 env1
Running virtualenv with interpreter
/usr/local/python-2
.7.8
/bin/python2
.7
New python executable
in
env1
/bin/python2
.7
Also creating executable
in
env1
/bin/python
Installing setuptools, pip...
done
.
|
Measured by default, the virtual environment does not rely on the global site-packages of the system environment. For example, the system environment installed MYSQLDB module, in the virtual environment import MySQLdb will prompt importerror. If you want to rely on a third-party package for your system environment, you can use parameter--system-site-packages. Alternatively, you can use the Virtualenvwrapper toggleglobalsitepackages command to control whether the current environment uses global site-packages.
123 |
[[email protected] ~] # virtualenv --system-site-packages env1 New python executable in env1 /bin/python Installing setuptools, pip... done . |
Third, start the virtual environment
Enter the virtual environment directory and start the virtual environment as follows:
1234 |
[[email protected] ~] # cd env1/ [[email protected] env1] # source bin/activate (env1)[[email protected] env1] # python -V Python 2.7.8 |
At this point the command line will be preceded by a parenthesis with the name of the virtual environment in parentheses. All modules installed later Easy_install or PIP will be installed in the virtual environment directory.
Iv. exiting the virtual environment
Exiting the virtual environment: deactivate
12 |
(env1)[[email protected] env1] # deactivate [[email protected] env1] # |
V. Use of Virtualenvwrapper
Virtualenvwrapper is a virtualenv extension tool that makes it easy to create, delete, copy, and switch between different virtual environments.
1. Installing Virtualenvwrapper
1 |
[[email protected] ~] # easy_install virtualenvwrapper |
Or:
1 |
[[email protected] ~] # pip install virtualenvwrapper |
Create a folder to hold all the virtual environments:
1 |
[[email protected] ~] # mkdir ~/workspaces |
Set the environment variable and add the following two lines to the ~/.BASHRC.
12 |
[[email protected] ~] # export WORKON_HOME=~/workspaces [[email protected] ~] # source /usr/bin/virtualenvwrapper.sh |
Then you can use the Virtualenvwrapper.
2. Create a virtual Environment: mkvirtualenv [Virtual Environment name]
1234567 |
[[email protected] ~]
# mkvirtualenv env1
New python executable
in
env1
/bin/python
Installing setuptools, pip...
done
.
(env1)[[email protected] ~]
# mkvirtualenv env2
New python executable
in
env2
/bin/python
Installing setuptools, pip...
done
.
(env2)[[email protected] ~]
#
|
Note: mkvirtualenv can use virtualenv parameters, such as--python, to specify the Python version. Once the virtual environment is created, it is automatically switched to this virtual environment. The virtual Environment directory is in Workon_home.
3. List Virtual environments: Lsvirtualenv-b
123 |
(env2)[[email protected] ~] # lsvirtualenv -b env1 env2 |
4. Switch virtual Environment: Workon [Virtual Environment name]
123 |
(env2)[[email protected] ~] # workon env1 (env1)[[email protected] ~] # echo $VIRTUAL_ENV /root/workspaces/env1 |
5. Check which packages are installed in the environment: lssitepackages
6. Enter the current environment directory: cdvirtualenv [subdirectory name]
123456 |
(env1)[[email protected] ~] # cdvirtualenv (env1)[[email protected] env1] # pwd /root/workspaces/env1 (env1)[[email protected] env1] # cdvirtualenv bin (env1)[[email protected] bin] # pwd /root/workspaces/env1/bin |
Enter the Site-packages directory for the current environment: cdsitepackages [subdirectory name]
123456 |
< Code class= "Bash plain" > (ENV1) [[EMAIL&NBSP;PROTECTED]&NBSP;ENV1] # Cdsitepackages (ENV1) [[Email protected] site-packages] # pwd /root/workspaces/env1/lib/python2 .6 /site-packages (ENV1) [[ Email protected] site-packages] # cdsitepackages pip (ENV1) [[Email protected] pip] # pwd /root/workspaces/env1/lib/python2 .6 /site-packages/pip |
7. Control whether the environment uses global site-packages:toggleglobalsitepackages
8. Replicating the Virtual environment: cpvirtualenv [Source] [dest]
123 |
[[email protected] ~] # cpvirtualenv env1 env3 Copying env1 as env3... (env3)[[email protected] ~] # |
9. Exit the virtual environment: deactivate
10. Delete Virtual Environment: rmvirtualenv [Virtual Environment name]
12 |
[[email protected] ~] # rmvirtualenv env2 Removing env2... |
Turn: Use virtualenv to build a separate Python environment