Virtualenv 的安装和使用

在python开发当中可能涉及到多个安装包和扩展包。比如Project A只引用了Package1, package2 ,而Project B 引用了 Package3, Package4。 如果我们在本地的python服务器上都安装上Package1, Package2, Package3, Package4,是否不是特别妥当?
为此,就有了Virtualenv这么一个东东出来了。Virtualenv是什么东西?引用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就是为了解决这一问题而出现的一个虚拟环境。下面我们来介绍一下如何安装和使用Virtualenv.

安装
    sudo pip install virtualenv
创建虚拟环境
  virtualenv scottPythonEnv

以上,就完成了一个虚拟environment的安装。我们可以在开发当中使用该环境。

注:使用Virtualenv我们很好的隔离了开发过程当中所使用的不同版本,解决了版本的冲突问题。

启动虚拟环境
    source bin/activate

我们再来看看我们的虚拟环境当中到底有什么内容。


1.png

2.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值