add by zhj: 在virtualenv环境下,安装第三方包时,不要用sudo pip install xxx,要直接用pip install xxx,若是用sudo时,那会安装在原来的系统Python目录下python
Eclipse的Pydev是支持virtualenv,virtualenv其实就是一个独立的Python解释器及相关的Python Package,你只要选中某个工程,而后配置一下该工程的Python解释器就能够了。编程
原文:http://www.nowamagic.net/academy/detail/1330228json
不一样的人喜欢用不一样的方式创建各自的开发环境,但在几乎全部的编程社区,总有一个(或一个以上)开发环境让人更容易接受。 使用不一样的开发环境虽然没有什么错误,但有些环境设置更容易进行便利的测试,并作一些重复/模板化的任务,使得在天天的平常工做简单并易于维护。ubuntu
在Python的开发环境的最经常使用的方法是使用 virtualenv 包。 Virtualenv是一个用来建立独立的Python环境的包。如今,出现了这样的问题:为何咱们须要一个独立的Python环境? 要回答这个问题,请容许我引用virtualenv本身的文档:app
virtualenv is a tool to create isolated Python environments.python2.7
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.测试
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.ui
Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host..net
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).code
咱们须要处理的基本问题是包的依赖、版本和间接权限问题。想象一下,你有两个应用,一个应用须要libfoo的版本1,而另外一应用须要版本2。如何才能同时使用这些应用程序?若是您安装到的/usr/lib/python2.7/site-packages(或任何平台的标准位置)的一切,在这种状况下,您可能会不当心升级不该该升级的应用程序。
简单地说,你能够为每一个项目创建不一样的/独立的Python环境,你将为每一个项目安装全部须要的软件包到它们各自独立的环境中。
安装 virtualenv 很简单:
1 |
pip install virtualenv |
virtualenv安装完毕后,能够经过运行下面的命令来为你的项目建立独立的python环境:
1 |
virtualenv --distribute nowamagic_venv |
OK,成功。上面发生了什么?他建立了文件夹 nowamagic_venv 来存储你的新的独立Python环境。 这个文件夹位于 /root 下面。
咱们再来看看输出:
1 |
New python executable in nowamagic_venv/bin/python2.7 |
2 |
Also creating executable in nowamagic_venv/bin/python |
3 |
Installing Setuptools...... done . |
4 |
Installing Pip........... done . |
--distribute 选项使virtualenv使用新的基于发行版的包管理系统而不是 setuptools 得到的包。 你如今须要知道的就是 --distribute 选项会自动在新的虚拟环境中安装 pip ,这样就不须要手动安装了。 当你成为一个更有经验的Python开发者,你就会明白其中细节。
经过下面的命令激活这个virtualenv:
1 |
[root@nowamagic ~] # cd nowamagic_venv |
2 |
[root@nowamagic nowamagic_venv] # source bin/activate |
3 |
(nowamagic_venv)[root@nowamagic nowamagic_venv] # |
运行下面的命令能够更好地理解二者的差别,若是已经进入virtualenv请先离开。
1 |
deactivate #离开 |
首先让咱们看看若是调用python/pip命令它会调用那一个。
1 |
[root@nowamagic ~] # which python |
2 |
/usr/bin/python |
1 |
[root@nowamagic ~] # which pip |
2 |
/usr/ local /bin/pip |
再来一次!此次打开virtualenv,而后看看有什么不一样。个人机子上显示以下:
1 |
[root@nowamagic ~] # which python |
2 |
/root/nowamagic_venv/bin/python |
1 |
[root@nowamagic ~] # which pip |
2 |
/root/nowamagic_venv/bin/pip |
virtualenv拷贝了Python可执行文件的副本,并建立一些有用的脚本和安装了项目须要的软件包,你能够在项目的整个生命周期中安装/升级/删除这些包。 它也修改了一些搜索路径,例如PYTHONPATH,以确保:
还有一点比较重要,在默认状况下,全部安装在系统范围内的包对于virtualenv是可见的。 这意味着若是你将simplejson安装在您的系统Python目录中,它会自动提供给全部的virtualenvs使用。 这种行为能够被更改,在建立virtualenv时增长 --no-site-packages 选项的virtualenv就不会读取系统包,以下(这里应该是说的site-packages目录下的包,对于ubuntu12.04,这个目录是空的,因此virtualenv始终不会使用系统Python目录下的第三方包。注:Ubuntu12.04的第三方包入在dist-packages目录下):
1 |
virtualenv nowamagic_venv --no-site-packages |