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

目录
相关文章
|
8月前
conda 环境安装
conda 环境安装
85 0
|
2月前
|
持续交付 开发工具 开发者
pip与virtualenv
【5月更文挑战第24天】本文介绍了Python开发中的模块和包管理工具pip与virtualenv。pip是Python的包管理器,用于安装、卸载、更新和列出包。virtualenv则提供了一个创建隔离的Python环境的工具,便于管理不同项目间的依赖关系。文章还提到了更高级的工具pipenv,它整合了pip和virtualenv的功能,支持自动化依赖管理、环境锁定,以及与Git、CI、Docker等的集成。通过学习和实践这些工具,开发者能更高效地管理Python项目。
11 1
|
Python
Anaconda虚拟环境安装Python库与Spyder
本文介绍在Anaconda中,为Python的虚拟环境安装第三方库与Spyder等配套软件的方法~
380 1
Anaconda虚拟环境安装Python库与Spyder
|
Shell Python
python虚拟环境(venv、virtualenv)及虚拟环境管理工具(virtualenvwrapper)
  Python应用开发时,本机的Python环境中安装各种各样的包的话,随着项目的增加,每次运行时都需要处理一下各种不同版本的依赖库,而且python版本也可能使用的不同,这样做很耗时。这就需要虚拟出不同的Python版本的环境,可以让虚拟环境专门为某一个应用而存在,且允许在虚拟环境中安装各种包。而且不影响本机大的python环境,便于开发环境和生产环境的管理。
384 0
|
Docker Python 容器
virtualenv
virtualenv
67 0
|
Python
pip、virtualenv、pyenv、pipenv等包管理工具简单区分
pip、virtualenv、pyenv、pipenv等包管理工具简单区分
pip、virtualenv、pyenv、pipenv等包管理工具简单区分
|
JSON 监控 安全
Python开发还在用virtualenv?是时候了解下pipenv了
过去大家经常使用virtualenv来创建虚拟环境,通过pip freeze生成requirements.txt文件,然后通过pip install -r requirements.txt进行项目模块的管理与安装。这样的安装存在很多问题,比如每次更新模块后,需要手动的重新生成依赖文件,等等问题。但看过今天的文章,你会更喜欢pipenv这款工具。
189 0
|
JSON 监控 安全
Python开发还在用virtualenv?不如了解下pipenv...
过去大家经常使用virtualenv来创建虚拟环境,通过pip freeze生成requirements.txt文件,然后通过pip install -r requirements.txt进行项目模块的管理与安装。这样的安装存在很多问题,比如每次更新模块后,需要手动的重新生成依赖文件,等等问题。但看过今天的文章,你会更喜欢pipenv这款工具。
347 0