不一样的人喜欢用不一样的方式创建各自的开发环境,但在几乎全部的编程社区,总有一个(或一个以上)开发环境让人更容易接受。 使用不一样的开发环境虽然没有什么错误,但有些环境设置更容易进行便利的测试,并作一些重复/模板化的任务,使得在天天的平常工做简单并易于维护。python
在Python的开发环境的最经常使用的方法是使用 virtualenv 包。 Virtualenv是一个用来建立独立的Python环境的包。如今,出现了这样的问题:为何咱们须要一个独立的Python环境? 要回答这个问题,请容许我引用virtualenv本身的文档:shell
virtualenv is a tool to create isolated Python environments.编程
The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.json
Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.app
Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host.python2.7
In all these cases, virtualenv can help you. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).测试
咱们须要处理的基本问题是包的依赖、版本和间接权限问题。想象一下,你有两个应用,一个应用须要libfoo的版本1,而另外一应用须要版本2。如何才能同时使用这些应用程序?若是您安装到的/usr/lib/python2.7/site-packages(或任何平台的标准位置)的一切,在这种状况下,您可能会不当心升级不该该升级的应用程序。ui
简单地说,你能够为每一个项目创建不一样的/独立的Python环境,你将为每一个项目安装全部须要的软件包到它们各自独立的环境中。.net
安装 virtualenv 很简单:orm
pip install virtualenv
virtualenv安装完毕后,能够经过运行下面的命令来为你的项目建立独立的python环境:
mkdir nowamagic_venv virtualenv --distribute nowamagic_venv
OK,成功。上面发生了什么?你建立了文件夹 nowamagic_venv 来存储你的新的独立Python环境。 这个文件夹位于 /root 下面。
咱们再来看看输出:
New python executable in nowamagic_venv/bin/python2.7 Also creating executable in nowamagic_venv/bin/python Installing Setuptools......done. Installing Pip...........done.
--distribute 选项使virtualenv使用新的基于发行版的包管理系统而不是 setuptools 得到的包。 你如今须要知道的就是 --distribute 选项会自动在新的虚拟环境中安装 pip ,这样就不须要手动安装了。 当你成为一个更有经验的Python开发者,你就会明白其中细节。
经过下面的命令激活这个virtualenv:
[root@nowamagic ~]# cd nowamagic_venv [root@nowamagic nowamagic_venv]# source bin/activate (nowamagic_venv)[root@nowamagic nowamagic_venv]#
运行下面的命令能够更好地理解二者的差别,若是已经进入virtualenv请先离开。
deactivate #离开
首先让咱们看看若是调用python/pip命令它会调用那一个。
[root@nowamagic ~]# which python /usr/bin/python
[root@nowamagic ~]# which pip /usr/local/bin/pip
再来一次!此次打开virtualenv,而后看看有什么不一样。个人机子上显示以下:
[root@nowamagic ~]# which python /root/nowamagic_venv/bin/python
[root@nowamagic ~]# which pip /root/nowamagic_venv/bin/pip
virtualenv拷贝了Python可执行文件的副本,并建立一些有用的脚本和安装了项目须要的软件包,你能够在项目的整个生命周期中安装/升级/删除这些包。 它也修改了一些搜索路径,例如PYTHONPATH,以确保:
还有一点比较重要,在默认状况下,全部安装在系统范围内的包对于virtualenv是可见的。 这意味着若是你将simplejson安装在您的系统Python目录中,它会自动提供给全部的virtualenvs使用。 这种行为能够被更改,在建立virtualenv时增长 --no-site-packages 选项的virtualenv就不会读取系统包,以下:
virtualenv nowamagic_venv --no-site-packages