引子
virtualenv是使用python的必备工具,本质上是创建一个虚拟环境,在这个环境中pip安装的模块,以及对python进行的改变,都不会对本机上的其他虚拟环境造成影响,这种隔离的思想在计算机编程中到处都有体现,可以实现系统性的解耦,方便调试。
科班出身的人大概早已掌握这个工具,西蒙宫是自学,所以知识体系不系统,所以在此写下virtualenv的使用方法,将来再次使用时方便查找。
安装方法
安装python开发包
对于CentOS
yum install python-devel
对于debain及ubuntu
apt-get install python-dev
安装virtualenv
pip install virtualenv
使用范例
创建一个ENV的范例,具体过程如下
root@virmach:~# virtualenv ENV New python executable in /root/ENV/bin/python Installing setuptools, pip, wheel...done.
文件树如下
root@virmach:~/ENV# tree -L 2 . |-- bin | |-- activate | |-- activate.csh | |-- activate.fish | |-- activate_this.py | |-- easy_install | |-- easy_install-2.7 | |-- pip | |-- pip2 | |-- pip2.7 | |-- python | |-- python-config | |-- python2 -> python | |-- python2.7 -> python | `-- wheel |-- include | `-- python2.7 -> /usr/include/python2.7 |-- lib | `-- python2.7 |-- local | |-- bin -> /root/ENV/bin | |-- include -> /root/ENV/include | `-- lib -> /root/ENV/lib `-- pip-selfcheck.json 9 directories, 15 files
激活virtualenv
root@virmach:~# source ./ENV/bin/activate (ENV) root@virmach:~#
出现了(ENV) 标志着你进入了这个虚拟环境,在这里是一个全新的python环境
于是就可以开始做你想要python做的任何事,做完之后,要关闭这个环境。
关闭virtualenv
(ENV) root@virmach:~# deactivate root@virmach:~#
可以看到(ENV) 不见了,这时又回到了系统的环境
获得帮助
仅有以上这些简短的介绍是不够的,那么获取帮助的方法是
root@virmach:~# virtualenv -h Usage: virtualenv [OPTIONS] DEST_DIR Options: --version show program's version number and exit -h, --help show this help message and exit -v, --verbose Increase verbosity. -q, --quiet Decrease verbosity. -p PYTHON_EXE, --python=PYTHON_EXE The Python interpreter to use, e.g., --python=python2.5 will use the python2.5 interpreter to create the new environment. The default is the interpreter that virtualenv was installed with (/usr/bin/python) --clear Clear out the non-root install and start from scratch. --no-site-packages DEPRECATED. Retained only for backward compatibility. Not having access to global site-packages is now the default behavior. --system-site-packages Give the virtual environment access to the global site-packages. --always-copy Always copy files rather than symlinking. --unzip-setuptools Unzip Setuptools when installing it. --relocatable Make an EXISTING virtualenv environment relocatable. This fixes up scripts and makes all .pth files relative. --no-setuptools Do not install setuptools in the new virtualenv. --no-pip Do not install pip in the new virtualenv. --no-wheel Do not install wheel in the new virtualenv. --extra-search-dir=DIR Directory to look for setuptools/pip distributions in. This option can be used multiple times. --download Download preinstalled packages from PyPI. --no-download, --never-download Do not download preinstalled packages from PyPI. --prompt=PROMPT Provides an alternative prompt prefix for this environment. --setuptools DEPRECATED. Retained only for backward compatibility. This option has no effect. --distribute DEPRECATED. Retained only for backward compatibility. This option has no effect. root@virmach:~#
感受
python真的是好用哦,这个工具可以完美实现一台机器同时运行python2.7和python3.5,有兴趣的朋友也可以搜索相关信息。