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

目录
相关文章
|
Windows Python Linux
virtualenv虚拟环境安装
C:\Users\Administrator>pip install virtualenv 安装: pip install virtualenv 进入虚拟环境命令: cd 所创建的虚拟环境的scripts 文件夹---> activate.
2001 0
|
虚拟化 Python
一篇文章完全理解virtualenv
1.virtualenv介绍 在python开发中,我们可能会遇到一种情况,就是当前的项目依赖的是某一个版本,但是另一个项目依赖的是另一个版本,这样就会造成依赖冲突,而virtualenv就是解决这种情况的,virtualenv通过创建一个虚拟化的python运行环境,将我们所需的依赖安装进去的,不同项目之间相互不干扰,如下所示。
3534 0
|
Docker Python 容器
virtualenv
virtualenv
110 0
|
9月前
|
持续交付 开发工具 开发者
pip与virtualenv
【5月更文挑战第24天】本文介绍了Python开发中的模块和包管理工具pip与virtualenv。pip是Python的包管理器,用于安装、卸载、更新和列出包。virtualenv则提供了一个创建隔离的Python环境的工具,便于管理不同项目间的依赖关系。文章还提到了更高级的工具pipenv,它整合了pip和virtualenv的功能,支持自动化依赖管理、环境锁定,以及与Git、CI、Docker等的集成。通过学习和实践这些工具,开发者能更高效地管理Python项目。
65 1
|
Shell Python
python虚拟环境(venv、virtualenv)及虚拟环境管理工具(virtualenvwrapper)
  Python应用开发时,本机的Python环境中安装各种各样的包的话,随着项目的增加,每次运行时都需要处理一下各种不同版本的依赖库,而且python版本也可能使用的不同,这样做很耗时。这就需要虚拟出不同的Python版本的环境,可以让虚拟环境专门为某一个应用而存在,且允许在虚拟环境中安装各种包。而且不影响本机大的python环境,便于开发环境和生产环境的管理。
462 0
|
1月前
|
Python
探索Python虚拟环境:virtualenv、venv与pipenv比较
在Python开发中,有效的环境管理至关重要。virtualenv、venv和pipenv是常用的虚拟环境管理工具。virtualenv支持Python 2.7+和3.3+,可创建独立环境;venv为Python 3.3+内置库,简单轻量但功能有限;pipenv则结合了包管理和虚拟环境管理,生成Pipfile.lock确保依赖确定性和安全性,推荐作为首选工具。

热门文章

最新文章