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

156人阅读 评论(0)

#### 什么是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).

#### 安装与使用virtualenv

 1 pip installvirtualenv 

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

 1 mkdir  nowamagic_venv
 2 virtualenv --distribute nowamagic_venv

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

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

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

﻿﻿
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：16498次
• 积分：595
• 等级：
• 排名：千里之外
• 原创：37篇
• 转载：13篇
• 译文：2篇
• 评论：1条