问题描述
我正在尝试使用此命令安装 Python 包
I am trying to install a Python package with this command
pip install <name of package>
我收到权限错误,我不知道为什么.我可以用 sudo
运行它,但有人告诉我这是个坏主意,我应该改用 virtualenv.
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.
什么是虚拟环境?它对我有什么作用?
What is a virtualenv? What does it do for me?
推荐答案
使用系统 Python 和库运行会将您限制为一个特定的 Python 版本,该版本由您的操作系统提供商选择.尝试在一个 Python 安装上运行所有 Python 应用程序很可能会在库集合之间发生版本冲突.对系统 Python 的更改也可能会破坏依赖它的其他操作系统功能.
Running with the system Python and libraries limits you to one specific Python version, chosen by your OS provider. Trying to run all Python applications on one Python installation makes it likely that version conflicts will occur among the collection of libraries. It's also possible that changes to the system Python will break other OS features that depend on it.
虚拟环境,或virtualenvs" 是轻量级、独立的 Python 安装,设计以最少的麻烦进行设置,并且无需大量配置或专业知识即可正常工作".
Virtual environments, or "virtualenvs" are lightweight, self-contained Python installations, designed to be set up with a minimum of fuss, and to "just work" without requiring extensive configuration or specialized knowledge.
virtualenv
避免了全局安装 Python 包的需要.当 virtualenv 处于活动状态时,pip
将在环境中安装包,这不会以任何方式影响基础 Python 安装.
virtualenv
avoids the need to install Python packages globally. When a virtualenv is active, pip
will install packages within the environment, which does not affect the base Python installation in any way.
在 Python 3.3 或更高版本中,您可以创建一个 virtualenv,如下所示:
In Python 3.3 or later, you can create a virtualenv as follows:
$ python3 -m venv ENV_DIR
对于 Windows,您应该将 python3
替换为 python.exe 的完整路径:
For Windows, you should replace python3
with the full path to python.exe:
>C:Python34python.exe -m venv ENV_DIR
(这是典型的 Python 安装;您的系统可能会有所不同.)
(This is a typical Python installation; your system may vary.)
在旧版本的 Python 中,包括 Python 2,以下命令之一在大多数情况下应该可以工作:
In older versions of Python, including Python 2, one of the following commands should work in most cases:
$ virtualenv ENV_DIR
$ venv ENV_DIR
$ pyvenv ENV_DIR
$ pyvenv3 ENV_DIR
ENV_DIR
应该是一个不存在的目录.该目录可以有任何名称,但为了使这些说明简单,我假设您已经在名为 venv
的目录中创建了 virtualenv(例如使用 python3 -m venv ./venv
).
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
).
要在您的 virtualenv 中工作,您需要激活它:
To work in your virtualenv, you activate it:
$ . ./venv/bin/activate
(venv)$
或者如果您有 Windows 系统,请使用它:
Or use this if you have a windows system:
$ venvScriptsactivate
shell 提示中的 (venv)
让你知道你激活了哪个 virtualenv,但如果你不喜欢它,你可以关闭这个功能.您可以运行所有常用的 Python 命令,它们将位于您的 virtualenv 本地:
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:
(venv)$ pip install requests numpy
[...]
(venv)$ python
[...]
>>> import requests
>>> import numpy as np
>>>
python
将运行您安装到 virtualenv 中的 Python 版本,因此(例如)您不必键入 python3
即可获得 Python 3.它运行的 Python 将可以访问所有标准库模块和您安装到 virtualenv 中的所有包,但(默认情况下)没有安装在系统范围的 site-packages
目录中的包.
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.
最后一条规则很重要:通过限制您的 virtualenv 仅使用本地安装的包,您可以确保您准确控制项目使用的依赖项,即使下周安装或更新了一些新的系统范围的包.如果您愿意,可以获取已安装软件包的列表:
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:
(venv)$ pip freeze
requests==2.13.0
numpy==1.12.0
(venv)$
pip
也可以解析这个格式并从中安装,它会安装相同的版本,即使在此期间已经发布了更新:
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:
(venv)$ pip freeze >requirements.txt
(some-other-venv)$ pip install -r requirements.txt
[...]
(some-other-venv)$ python
>>> import requests
>>> import numpy as np
>>>
您可以通过停用 virtualenv 来退出它:
You can get out of the virtualenv by deactivating it:
(venv)$ deactivate
$ python
[...]
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'requests'
您可以创建任意数量的虚拟环境,它们不会相互干扰,也不会干扰您的系统包.virtualenv 是只是"一个包含一堆二进制文件和脚本的目录,因此您可以像删除任何目录一样删除 virtualenv(rm -r venv
在 Unix 上).如果在删除 virtualenv 时激活了它,你可能会混淆你的 shell,所以在这种情况下,首先 deactivate
可能是个好主意.
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,我为什么要使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!