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.
Easy_install Installation Method:
[[email protected] ~]# Yum install python-setuptools python-devel[[email protected] ~]# easy_install virtualenv
Pip installation Method:
[[email protected] ~]# easy_install pip[[email protected] ~]# pip install virtualenv
Yum installation mode (Epel source):
[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].
[Email protected] ~]# virtualenv env1new python executable in env1/bin/pythoninstalling 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:
[Email protected] ~]# virtualenv--python=/usr/local/python-2.7.8/bin/python2.7 env1running virtualenv with Interpreter/usr/local/python-2.7.8/bin/python2.7new python executable in env1/bin/python2.7also creating executable In Env1/bin/pythoninstalling 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.
[Email protected] ~]# virtualenv--system-site-packages env1new python executable in env1/bin/pythoninstalling Setuptools, Pip...done.
Third, start the virtual environment
Enter the virtual environment directory and start the virtual environment as follows:
[Email protected] ~]# CD Env1/[[email protected] env1]# source Bin/activate (ENV1) [[email protected] env1]# Python-vpytho N 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
(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
[Email protected] ~]# Easy_install virtualenvwrapper
Or:
[[Email protected] ~]# pip install Virtualenvwrapper
Create a folder to hold all the virtual environments:
[Email protected] ~]# mkdir ~/workspaces
Set the environment variable and add the following two lines to the ~/.BASHRC.
[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]
[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
(ENV2) [Email protected] ~]# Lsvirtualenv-benv1env2
4. Switch virtual Environment:Workon [Virtual environment name]
(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]
(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]
(ENV1) [Email protected] 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]
[Email protected] ~]# cpvirtualenv env1 env3copying env1 as Env3 ... (ENV3) [Email protected] ~]#
9. Exit the virtual Environment:Deactivate
10. Delete Virtual Environment:rmvirtualenv [Virtual environment name]
[Email protected] ~]# rmvirtualenv env2removing env2 ...
This article is from the "Departure-linux Technology Blog" blog, please be sure to keep this source http://qicheng0211.blog.51cto.com/3958621/1561685
CentOS uses virtualenv to build a separate Python environment