1、virtualevn是什么?
virtualenv is a Python tool written by Ian Bicking and used to create isolated environments for Python in which you can install packages without interfering with the other virtualenvs nor with the system Python's packages. The present article covers the installation of the virtualenv package and its companion command line utility virtualenvwrapper designed by Doug Hellmann to (greatly) improve your work flow. A quick how-to to help you to begin working inside virtual environment is then provided.--摘自维基百科
2、virtualevn的安装
1) 使用pip安装
pip install virtualenv
2) easy_install 安装
sudo easy_install virtualenv
3)ubuntu/debian系列
sudo apt-get install python-virtualenv
4)redhat/centos系列
sudo yum install python-virtualenv
3、virtualenv的简单使用
1)创建一个虚拟环境
virtualenv mytest
之后,就可以看到在执行命令的当前目录下存在了一个mytest目录,进入该目录发现有如下的文件
[root@puppetmaster mytest]# ll
total 12
drwxr-xr-x 2 root root 4096 Sep 28 22:22 bin
drwxr-xr-x 2 root root 4096 Sep 28 22:22 include
drwxr-xr-x 3 root root 4096 Sep 28 22:22 lib
lrwxrwxrwx 1 root root 3 Sep 28 22:22 lib64 -> lib
看下bin目录下有哪些文件:
[root@puppetmaster mytest]# ll bin/
total 40
-rw-r--r-- 1 root root 2193 Sep 28 22:22 activate
-rw-r--r-- 1 root root 1249 Sep 28 22:22 activate.csh
-rw-r--r-- 1 root root 2388 Sep 28 22:22 activate.fish
-rw-r--r-- 1 root root 1129 Sep 28 22:22 activate_this.py
-rwxr-xr-x 1 root root 328 Sep 28 22:22 easy_install
-rwxr-xr-x 1 root root 336 Sep 28 22:22 easy_install-2.6
-rwxr-xr-x 1 root root 289 Sep 28 22:22 pip
-rwxr-xr-x 1 root root 297 Sep 28 22:22 pip-2.6
-rwxr-xr-x 1 root root 4864 Sep 28 22:22 python
lrwxrwxrwx 1 root root 6 Sep 28 22:22 python2 -> python
lrwxrwxrwx 1 root root 6 Sep 28 22:22 python2.6 -> python
可以看到,在virtualenv工具为我们创建的mytest目录下,已经自动为我们安装了很多工具,包括pip以及跟系统python一样的python版本。默认情况下,虚拟环境会依赖系统环境中的site packages,就是说系统中已经安装好的第三方package也会安装在虚拟环境中,如果不想依赖这些package,那么可以加上参数--no-site-packages,也就变成了这样:
virtualenv mytest --no-site-packages
2)激活(启动)该虚拟环境
source ./bin/activate
之后,我们发现,我们的操作行前面多了mytest字样,如下所示:
(mytest)[root@puppetmaster mytest]#
3)退出虚拟环境
deactivate