用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
    评论
Python virtualenv是一个用来创建和管理Python虚拟环境的工具。虚拟环境是一个独立Python运行环境,可以用来隔离不同项目的依赖关系和包版本。通过使用虚拟环境,可以在同一台主机上同时运行多个项目,每个项目都有自己独立Python解释器和安装的软件包。 要使用virtualenv创建虚拟环境,首先需要安装virtualenv工具。可以使用pip命令安装,例如在命令行中输入"pip install virtualenv"。安装完成后,可以使用以下命令创建虚拟环境: virtualenv [虚拟环境名称] 例如,运行"virtualenv myenv"将创建一个名为myenv的虚拟环境。 要激活虚拟环境,首先需要进入虚拟环境所在的目录,在命令行中输入"activate"。这将激活虚拟环境,并将命令行提示符修改为虚拟环境名称。要退出虚拟环境,可以在命令行中输入"deactivate"。 使用虚拟环境可以避免不同项目之间的包冲突,并且可以方便地管理每个项目的依赖关系。通过为每个项目创建独立的虚拟环境,可以确保项目之间的独立性和隔离性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [通过pyenv和virtualenv搭建python开发环境](https://blog.csdn.net/hao_zhang_shrek/article/details/100054170)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python之虚拟环境【virtualenv】](https://blog.csdn.net/qq_35844043/article/details/81842060)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值