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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容