用virtualenv建立多个Python独立开发环境

整理自: http://www.nowamagic.net/academy/detail/1330228

什么是virtualenv?

在Python的开发环境的最常用的方法是使用 virtualenv 包。 Virtualenv是一个用来创建独立的Python环境的包。现在,出现了这样的问题:为什么我们需要一个独立的Python环境? 要回答这个问题,请允许我引用virtualenv自己的文档:

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.

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.

Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host.

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(或任何平台的标准位置)的一切,在这种情况下,您可能会不小心升级不应该升级的应用程序。

简单地说,你可以为每个项目建立不同的/独立的Python环境,你将为每个项目安装所有需要的软件包到它们各自独立的环境中。

安装与使用virtualenv

安装 virtualenv 很简单:

1pip installvirtualenv

virtualenv安装完毕后,可以通过运行下面的命令来为你的项目创建独立的python环境:

1mkdir nowamagic_venv
2virtualenv --distribute nowamagic_venv

OK,成功。上面发生了什么?你创建了文件夹 nowamagic_venv 来存储你的新的独立Python环境。 这个文件夹位于 /root 下面。

我们再来看看输出:

1New python executable innowamagic_venv/bin/python2.7
2Also creating executable innowamagic_venv/bin/python
3Installing Setuptools......done.
4Installing Pip...........done.

--distribute 选项使virtualenv使用新的基于发行版的包管理系统而不是 setuptools 获得的包。 你现在需要知道的就是 --distribute 选项会自动在新的虚拟环境中安装 pip ,这样就不需要手动安装了。 当你成为一个更有经验的Python开发者,你就会明白其中细节。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值