什么是virtualenv,我为什么要使用它?

时间:2022-09-16 14:13:15

I am trying to install a Python package with this command

我正在尝试使用此命令安装Python包

pip install <name of package>

I'm getting permission errors and I'm not sure why. I could run it with sudo, but someone told me that was a bad idea, and I should use a virtualenv instead.

我收到了许可错误,我不知道为什么。我可以用sudo运行它,但有人告诉我这是一个坏主意,我应该使用virtualenv代替。

What is a virtualenv? What does it do for me?

什么是virtualenv?它对我有什么用?

3 个解决方案

#1


32  

A virtualenv is a lightweight, self-contained Python installation. They are designed to be set up with a minimum of fuss, and to "just work" without requiring extensive configuration or specialized knowledge.

virtualenv是一个轻量级,自包含的Python安装。它们的设计目的很简单,“只需工作”,无需大量配置或专业知识。

In Python 3.3 or later, you can create a virtualenv as follows:

在Python 3.3或更高版本中,您可以创建virtualenv,如下所示:

$ python3 -m venv ENV_DIR

For Windows, you should replace python3 with the full path to python.exe:

对于Windows,您应该将python3替换为python.exe的完整路径:

>C:\Python34\python.exe -m venv ENV_DIR

(This is a typical Python installation; your system may vary.)

(这是典型的Python安装;您的系统可能会有所不同。)

In older versions of Python, including Python 2, one of the following commands should work in most cases:

在旧版本的Python(包括Python 2)中,以下命令之一应该适用于大多数情况:

$ virtualenv ENV_DIR
$ venv ENV_DIR
$ pyvenv ENV_DIR
$ pyvenv3 ENV_DIR

ENV_DIR should be a non-existent directory. The directory can have any name, but to keep these instructions simple, I will assume you have created your virtualenv in a directory called venv (e.g. with python3 -m venv ./venv).

ENV_DIR应该是一个不存在的目录。该目录可以有任何名称,但为了简化这些说明,我假设您已经在名为venv的目录中创建了virtualenv(例如使用python3 -m venv ./venv)。

To work in your virtualenv, you activate it:

要在virtualenv中工作,请激活它:

$ . ./venv/bin/activate
(venv)$ 

Or use this if you have a windows system:

如果你有一个Windows系统,请使用它:

$ venv\Scripts\activate

The (venv) in the shell prompt lets you know which virtualenv you have activated, but you can turn this feature off if you do not like it. You can run all the usual Python commands, and they will be local to your virtualenv:

shell提示符中的(venv)可以让您知道已激活的virtualenv,但如果您不喜欢它,可以关闭此功能。你可以运行所有常用的Python命令,它们将是你的virtualenv的本地命令:

(venv)$ pip install requests numpy
[...]
(venv)$ python
[...]
>>> import requests
>>> import numpy as np
>>> 

python will run the version of Python that you installed into your virtualenv, so (for example) you don't have to type python3 to get Python 3. The Python that it runs will have access to all the standard library modules and all the packages you installed into the virtualenv, but (by default) none of the packages installed in the system-wide site-packages directory.

python将运行您安装到virtualenv中的Python版本,因此(例如)您不必键入python3来获取Python 3.它运行的Python将可以访问所有标准库模块和所有包您安装到virtualenv中,但(默认情况下)没有安装在系统范围的site-packages目录中的软件包。

This last rule is important: by restricting your virtualenv to only use locally-installed packages, you can ensure that you control exactly which dependencies your project is using, even if some new system-wide package gets installed or updated next week. If you like, you can get a listing of your installed packages:

最后一条规则很重要:通过将virtualenv限制为仅使用本地安装的软件包,您可以确保控制项目正在使用的确切依赖项,即使下周安装或更新了一些新的系统范围软件包。如果您愿意,可以获得已安装软件包的列表:

(venv)$ pip freeze
requests==2.13.0
numpy==1.12.0
(venv)$ 

pip can also parse this format and install from it, and it will install the same versions, even if updates have been released in the meantime:

pip也可以解析这种格式并从中安装,它会安装相同的版本,即使在此期间发布了更新:

(venv)$ pip freeze >requirements.txt

(some-other-venv)$ pip install -r requirements.txt
[...]
(some-other-venv)$ python
>>> import requests
>>> import numpy as np
>>> 

You can get out of the virtualenv by deactivating it:

你可以通过停用它来摆脱virtualenv:

(venv)$ deactivate
$ python
[...]
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'requests'

You can create as many virtualenvs as you like, and they won't interfere with each other, nor with your system packages. A virtualenv is "just" a directory with a bunch of binaries and scripts under it, so you can remove a virtualenv the same way you remove any directory (rm -r venv on Unix). If the virtualenv is activated when you remove it, you may confuse your shell, so it's probably a good idea to deactivate first in that case.

您可以根据需要创建任意数量的virtualenv,它们不会相互干扰,也不会与系统包相互干扰。 virtualenv“只是”一个目录下面有一堆二进制文件和脚本,所以你可以像删除任何目录(在Unix上使用rm -r venv)一样删除virtualenv。如果virtualenv在您删除时被激活,您可能会混淆shell,因此在这种情况下首先停用它可能是个好主意。

#2


4  

Some times you are not given root privileges and you might end up not being able to use sudo. Many other times, it's not advisable to use sudo to install packages as it might overwrite some package which might be in use by some other applications.

有时您没有获得root权限,最终可能无法使用sudo。许多其他时候,不建议使用sudo来安装软件包,因为它可能会覆盖某些其他应用程序可能正在使用的软件包。

Virtualenv can help you create a separate environment where you don't need root privileges as well as be able to tailor the environment according to your need. It consists of self-contained python installation which only interacts with your specific created environment.

Virtualenv可以帮助您创建一个单独的环境,您不需要root权限,也可以根据需要定制环境。它由自包含的python安装组成,它只与您特定的创建环境进行交互。

So basically, it gives you a bit of freedom as well as avoid damaging (or modifying) the root environment which might be hosting many old functionalities of old applications.

基本上,它为您提供了一些*,同时避免破坏(或修改)可能托管旧应用程序的许多旧功能的根环境。

Installation is pretty easy too.

安装也很简单。

#3


0  

Installing packages with pip and sudo will install packages globally, which may break some system tools.

使用pip和sudo安装软件包将全局安装软件包,这可能会破坏一些系统工具。

By install globally it means you will install your packages in place like /usr/lib/python2.7/site-package so if some packages need a previous version of your python packages, this action may break it.

通过全局安装,这意味着您将安装您的软件包,如/usr/lib/python2.7/site-package,因此如果某些软件包需要以前版本的python软件包,此操作可能会破坏它。

virtualenv allows you to avoid installing Python packages globally by make an isolated python environments. That means it will install packages just in your desire project folder.

virtualenv允许您通过创建一个隔离的python环境来避免全局安装Python包。这意味着它将在您想要的项目文件夹中安装软件包。

On mac and linux

在mac和linux上

  • Install

python3 -m pip install --user virtualenv

python3 -m pip install --user virtualenv

  • Creating a Virtual Env: Go to your desire project folder
  • 创建虚拟环境:转到您想要的项目文件夹

python3 -m virtualenv env

python3 -m virtualenv env

  • Activating a virtualenv: In your desire project folder
  • 激活virtualenv:在您的欲望项目文件夹中

source env/bin/activate

After activating you could install your packages using pip

激活后,您可以使用pip安装软件包

For more information about using it in Windows: How to use virtaulen in Windows

有关在Windows中使用它的更多信息:如何在Windows中使用virtaulen

#1


32  

A virtualenv is a lightweight, self-contained Python installation. They are designed to be set up with a minimum of fuss, and to "just work" without requiring extensive configuration or specialized knowledge.

virtualenv是一个轻量级,自包含的Python安装。它们的设计目的很简单,“只需工作”,无需大量配置或专业知识。

In Python 3.3 or later, you can create a virtualenv as follows:

在Python 3.3或更高版本中,您可以创建virtualenv,如下所示:

$ python3 -m venv ENV_DIR

For Windows, you should replace python3 with the full path to python.exe:

对于Windows,您应该将python3替换为python.exe的完整路径:

>C:\Python34\python.exe -m venv ENV_DIR

(This is a typical Python installation; your system may vary.)

(这是典型的Python安装;您的系统可能会有所不同。)

In older versions of Python, including Python 2, one of the following commands should work in most cases:

在旧版本的Python(包括Python 2)中,以下命令之一应该适用于大多数情况:

$ virtualenv ENV_DIR
$ venv ENV_DIR
$ pyvenv ENV_DIR
$ pyvenv3 ENV_DIR

ENV_DIR should be a non-existent directory. The directory can have any name, but to keep these instructions simple, I will assume you have created your virtualenv in a directory called venv (e.g. with python3 -m venv ./venv).

ENV_DIR应该是一个不存在的目录。该目录可以有任何名称,但为了简化这些说明,我假设您已经在名为venv的目录中创建了virtualenv(例如使用python3 -m venv ./venv)。

To work in your virtualenv, you activate it:

要在virtualenv中工作,请激活它:

$ . ./venv/bin/activate
(venv)$ 

Or use this if you have a windows system:

如果你有一个Windows系统,请使用它:

$ venv\Scripts\activate

The (venv) in the shell prompt lets you know which virtualenv you have activated, but you can turn this feature off if you do not like it. You can run all the usual Python commands, and they will be local to your virtualenv:

shell提示符中的(venv)可以让您知道已激活的virtualenv,但如果您不喜欢它,可以关闭此功能。你可以运行所有常用的Python命令,它们将是你的virtualenv的本地命令:

(venv)$ pip install requests numpy
[...]
(venv)$ python
[...]
>>> import requests
>>> import numpy as np
>>> 

python will run the version of Python that you installed into your virtualenv, so (for example) you don't have to type python3 to get Python 3. The Python that it runs will have access to all the standard library modules and all the packages you installed into the virtualenv, but (by default) none of the packages installed in the system-wide site-packages directory.

python将运行您安装到virtualenv中的Python版本,因此(例如)您不必键入python3来获取Python 3.它运行的Python将可以访问所有标准库模块和所有包您安装到virtualenv中,但(默认情况下)没有安装在系统范围的site-packages目录中的软件包。

This last rule is important: by restricting your virtualenv to only use locally-installed packages, you can ensure that you control exactly which dependencies your project is using, even if some new system-wide package gets installed or updated next week. If you like, you can get a listing of your installed packages:

最后一条规则很重要:通过将virtualenv限制为仅使用本地安装的软件包,您可以确保控制项目正在使用的确切依赖项,即使下周安装或更新了一些新的系统范围软件包。如果您愿意,可以获得已安装软件包的列表:

(venv)$ pip freeze
requests==2.13.0
numpy==1.12.0
(venv)$ 

pip can also parse this format and install from it, and it will install the same versions, even if updates have been released in the meantime:

pip也可以解析这种格式并从中安装,它会安装相同的版本,即使在此期间发布了更新:

(venv)$ pip freeze >requirements.txt

(some-other-venv)$ pip install -r requirements.txt
[...]
(some-other-venv)$ python
>>> import requests
>>> import numpy as np
>>> 

You can get out of the virtualenv by deactivating it:

你可以通过停用它来摆脱virtualenv:

(venv)$ deactivate
$ python
[...]
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'requests'

You can create as many virtualenvs as you like, and they won't interfere with each other, nor with your system packages. A virtualenv is "just" a directory with a bunch of binaries and scripts under it, so you can remove a virtualenv the same way you remove any directory (rm -r venv on Unix). If the virtualenv is activated when you remove it, you may confuse your shell, so it's probably a good idea to deactivate first in that case.

您可以根据需要创建任意数量的virtualenv,它们不会相互干扰,也不会与系统包相互干扰。 virtualenv“只是”一个目录下面有一堆二进制文件和脚本,所以你可以像删除任何目录(在Unix上使用rm -r venv)一样删除virtualenv。如果virtualenv在您删除时被激活,您可能会混淆shell,因此在这种情况下首先停用它可能是个好主意。

#2


4  

Some times you are not given root privileges and you might end up not being able to use sudo. Many other times, it's not advisable to use sudo to install packages as it might overwrite some package which might be in use by some other applications.

有时您没有获得root权限,最终可能无法使用sudo。许多其他时候,不建议使用sudo来安装软件包,因为它可能会覆盖某些其他应用程序可能正在使用的软件包。

Virtualenv can help you create a separate environment where you don't need root privileges as well as be able to tailor the environment according to your need. It consists of self-contained python installation which only interacts with your specific created environment.

Virtualenv可以帮助您创建一个单独的环境,您不需要root权限,也可以根据需要定制环境。它由自包含的python安装组成,它只与您特定的创建环境进行交互。

So basically, it gives you a bit of freedom as well as avoid damaging (or modifying) the root environment which might be hosting many old functionalities of old applications.

基本上,它为您提供了一些*,同时避免破坏(或修改)可能托管旧应用程序的许多旧功能的根环境。

Installation is pretty easy too.

安装也很简单。

#3


0  

Installing packages with pip and sudo will install packages globally, which may break some system tools.

使用pip和sudo安装软件包将全局安装软件包,这可能会破坏一些系统工具。

By install globally it means you will install your packages in place like /usr/lib/python2.7/site-package so if some packages need a previous version of your python packages, this action may break it.

通过全局安装,这意味着您将安装您的软件包,如/usr/lib/python2.7/site-package,因此如果某些软件包需要以前版本的python软件包,此操作可能会破坏它。

virtualenv allows you to avoid installing Python packages globally by make an isolated python environments. That means it will install packages just in your desire project folder.

virtualenv允许您通过创建一个隔离的python环境来避免全局安装Python包。这意味着它将在您想要的项目文件夹中安装软件包。

On mac and linux

在mac和linux上

  • Install

python3 -m pip install --user virtualenv

python3 -m pip install --user virtualenv

  • Creating a Virtual Env: Go to your desire project folder
  • 创建虚拟环境:转到您想要的项目文件夹

python3 -m virtualenv env

python3 -m virtualenv env

  • Activating a virtualenv: In your desire project folder
  • 激活virtualenv:在您的欲望项目文件夹中

source env/bin/activate

After activating you could install your packages using pip

激活后,您可以使用pip安装软件包

For more information about using it in Windows: How to use virtaulen in Windows

有关在Windows中使用它的更多信息:如何在Windows中使用virtaulen