Python 如何在 virtualenv 中安装包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21240653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:22:58  来源:igfitidea点击:

How to install a package inside virtualenv?

pythonvirtualenvpip

提问by user1424739

I created a virtualenv with the following command.

我使用以下命令创建了一个 virtualenv。

mkvirtualenv --distribute --system-site-packages ""

After starting the virtualenv with workon, I type ipython. It prompts me

用 启动 virtualenv 后workon,我输入ipython. 它提示我

WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.

When I try to install ipython with the virtualenv, I got the following error message:

当我尝试使用 virtualenv 安装 ipython 时,收到以下错误消息:

pip install ipython
Requirement already satisfied (use --upgrade to upgrade): ipython in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Cleaning up...

Does anyone know how to install inside the virtualenv?

有谁知道如何在virtualenv中安装?

回答by PepperoniPizza

To use the environment virtualenv has created, you first need to source env/bin/activate. After that, just install packages using pip install package-name.

要使用 virtualenv 创建的环境,您首先需要source env/bin/activate. 之后,只需使用pip install package-name.

回答by Charles Duffy

Create your virtualenv with --no-site-packagesif you don't want it to be able to use external libraries:

--no-site-packages如果您不希望它能够使用外部库,请创建您的 virtualenv :

virtualenv --no-site-packages my-virtualenv
. my-virtualenv/bin/activate
pip install ipython

Otherwise, as in your example, it can see a library installed in your system Python environment as satisfying your requested dependency.

否则,就像在您的示例中一样,它可以看到系统 Python 环境中安装的库满足您请求的依赖项。

回答by Christian Abbott

To further clarify the other answer here:

为了进一步澄清这里的另一个答案:

Under the current version of virtualenv, the --no-site-packages flag is the default behavior, so you don't need to specify it. However, you are overriding the default by explicitly using the --system-site-packages flag, and that's probably not what you want. The default behavior (without specifying either flag) is to create the virtual environment such that when you are using it, any Python packages installed outside the environment are not accessible. That's typically the right choice because it best isolates the virtual environment from your local computer environment. Python packages installed within the environment will not affect your local computer and vice versa.

在当前版本的 virtualenv 下, --no-site-packages 标志是默认行为,因此您不需要指定它。但是,您通过显式使用 --system-site-packages 标志来覆盖默认值,这可能不是您想要的。默认行为(不指定任何一个标志)是创建虚拟环境,这样当您使用它时,安装在环境之外的任何 Python 包都无法访问。这通常是正确的选择,因为它最好地将虚拟环境与本地计算机环境隔离开来。安装在环境中的 Python 包不会影响您的本地计算机,反之亦然。

Secondly, to use a virtual environment after it's been created, you need to navigate into the virtual environment directory and then run:

其次,要在创建后使用虚拟环境,您需要导航到虚拟环境目录,然后运行:

bin/activate

What this does is to configure environment variables so that Python packages and any executables in the virtual environment's bin folders will be used before those in the standard locations on your local computer. So, for example, when you type "pip", the version of pip that is inside your virtual environment will run instead of the version of pip on your local machine. This is desirable because pip inside the virtual environment will install packages inside the virtual environment.

这样做是为了配置环境变量,以便 Python 包和虚拟环境 bin 文件夹中的任何可执行文件将在本地计算机上标准位置中的那些之前使用。因此,例如,当您键入“pip”时,虚拟环境中的 pip 版本将运行,而不是本地计算机上的 pip 版本。这是可取的,因为虚拟环境中的 pip 会在虚拟环境中安装包。

The problem you are having is because you are running programs (like ipython) from your local machine, when you instead want to install and run copies of those programs isolated inside your virtual environment. You set this up by creating the environment (without specifying any site-packages flags if you are using the current version), running the activate script mentioned above, then running pip to install any packages you need (which will go inside the environment).

您遇到的问题是因为您正在本地计算机上运行程序(如 ipython),而您想要安装和运行在虚拟环境中隔离的那些程序的副本。您可以通过创建环境(如果您使用当前版本,则不指定任何站点包标志)来设置它,运行上面提到的激活脚本,然后运行 ​​pip 来安装您需要的任何包(这将进入环境)。

回答by Ja8zyjits

Well i don't have an appropriate reason regarding why this behavior occurs but then i just found a small work around

好吧,关于为什么会发生这种行为,我没有适当的理由,但后来我找到了一个小的解决方法

Inside the VirtualEnvironment

虚拟环境内部

pip install -Iv package_name==version_number

now this will install the version in your virtual environment

现在这将在您的虚拟环境中安装该版本

Additionally you can check inside the virtual environment with this

此外,您可以使用此检查虚拟环境内部

pip install yolk
yolk -l

This shall give you the details of all the installed packages in both the locations(system and virtualenv)

这将为您提供两个位置(system 和 virtualenv)中所有已安装软件包的详细信息

While some might say its not appropriate to use --system-site-packages (it may be true), but what if you have already done a lot of stuffs inside your virtualenv? Now you dont want to redo everything from the scratch.

虽然有些人可能会说使用 --system-site-packages 不合适(这可能是真的),但是如果你已经在你的 virtualenv 中做了很多事情怎么办?现在您不想从头开始重做所有事情。

You may use this as a hack and be careful from the next time :)

您可以将其用作黑客,下次要小心:)

回答by moovon

For Python 3 :

对于 Python 3:

pip3 install virtualenv

python3 -m venv venv_name

source venv_name/bin/activate  #key step

pip3 install "package-name"

回答by Max Caldwell

I had the same issue and the --no-site-packagesdid not work for me. I discovered on this older mailing list archive that you are able to force an installation in the virtualenv using the -Uflag for pip, eg pip -U ipython. You may verify this works using the bash command which ipythonwhile in the virtualenv.

我有同样的问题,但--no-site-packages对我不起作用。我在这个较旧的邮件列表存档中发现,您可以使用-Upip 标志强制在 virtualenv 中进行安装,例如pip -U ipython. 您可以which ipython在 virtualenv 中使用 bash 命令验证这是否有效。

source: https://mail.python.org/pipermail/python-list/2010-March/571663.html

来源:https: //mail.python.org/pipermail/python-list/2010-March/571663.html

回答by ivanleoncz

Avoiding Headaches and Best Practices:

避免头痛和最佳实践:

  • Virtual Environments are not part of your git project (they don't need to be versioned) !

  • They can reside on the project folder (locally), but, ignored on your .gitignore.

  • After activating the virtual environment of your project, never "sudopip install package".
  • After finishing your work, always "deactivate" your environment.
  • Avoid renaming your project folder.
  • 虚拟环境不是您的 git 项目的一部分(它们不需要进行版本控制)!

  • 它们可以驻留在项目文件夹中(本地),但在您的.gitignore.

  • 激活项目的虚拟环境后,千万不要“ sudo pip install package”。
  • 完成工作后,始终“停用”您的环境。
  • 避免重命名您的项目文件夹。



For a better representation, here's a simulation:


为了更好地表示,这是一个模拟:

creating a folder for your projects/environments

为您的项目/环境创建一个文件夹

$ mkdir venv

creating environment

创造环境

$ cd venv/ 

$ virtualenv google_drive
New python executable in google_drive/bin/python
Installing setuptools, pip...done.

activating environment

活化环境

$ source google_drive/bin/activate

installing packages

安装包

(google_drive) $ pip install PyDrive
Downloading/unpacking PyDrive
Downloading PyDrive-1.3.1-py2-none-any.whl
...
...
...    
Successfully installed PyDrive PyYAML google-api-python-client oauth2client six uritemplate httplib2 pyasn1 rsa pyasn1-modules
Cleaning up...

package available inside the environment

环境中可用的包

(google_drive) $ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import pydrive.auth
>>>  
>>> gdrive = pydrive.auth.GoogleAuth()
>>>

deactivate environment

停用环境

(google_drive) $ deactivate 

$ 

package NOT AVAILABLE outside the environment

包在环境之外不可用

$ python
Python 2.7.6 (default, Oct 26 2016, 20:32:10) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import pydrive.auth
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pydrive.auth
>>> 


Notes:

笔记:

Why not sudo?

为什么不是sudo?

Virtualenv creates a whole new environment for you, defining $PATH and some other variables and settings. When you use sudo pip install package, you are running Virtualenv as root, escaping the whole environment which was created, and then, installing the package on global site-packages, and not inside the project folderwhere you have a Virtual Environment, although you have activated the environment.

Virtualenv 为您创建了一个全新的环境,定义了 $PATH 和其他一些变量和设置。当您使用sudo pip install package 时,您以root身份运行 Virtualenv ,转义创建的整个环境,然后在全局站点包上安装包,而不是在您拥有虚拟环境的项目文件夹中,尽管您已经激活了环境。

If you rename the folder of your project...

如果您重命名项目的文件夹...

...you'll have to adjust some variables from some files inside the bindirectory of your project.

For example:

bin/pip, line 1 (She Bang)

bin/activate, line 42 (VIRTUAL_ENV)

...您必须从项目的bin目录中的某些文件中调整一些变量。

例如:

bin/pip, line 1 (She Bang)

bin/activate,第 42 行(VIRTUAL_ENV)

回答by SergO

From documentation https://docs.python.org/3/library/venv.html:

从文档https://docs.python.org/3/library/venv.html

The pyvenv script has been deprecated as of Python 3.6 in favor of using python3 -m venv to help prevent any potential confusion as to which Python interpreter a virtual environment will be based on.

从 Python 3.6 开始,pyvenv 脚本已被弃用,转而使用 python3 -m venv 来帮助防止任何潜在的混淆,即虚拟环境将基于哪个 Python 解释器。

In order to create a virtual environment for particular project, create a file /home/user/path/to/create_venv.sh:

为了为特定项目创建虚拟环境,请创建一个文件/home/user/path/to/create_venv.sh

#!/usr/bin/env bash

# define path to your project's directory
PROJECT_DIR=/home/user/path/to/Project1

# a directory with virtual environment
# will be created in your Project1 directory
# it recommended to add this path into your .gitignore
VENV_DIR="${PROJECT_DIR}"/venv

# https://docs.python.org/3/library/venv.html
python3 -m venv "${VENV_DIR}"

# activates the newly created virtual environment
. "${VENV_DIR}"/bin/activate

# prints activated version of Python
python3 -V

pip3 install --upgrade pip

# Write here all Python libraries which you want to install over pip
# An example or requirements.txt see here:
# https://docs.python.org/3/tutorial/venv.html#managing-packages-with-pip
pip3 install -r "${PROJECT_DIR}"/requirements.txt

echo "Virtual environment ${VENV_DIR} has been created"

deactivate

Then run this script in the console:

然后在控制台中运行这个脚本:

$ bash /home/user/path/to/create_venv.sh

回答by Nikhil VJ

Sharing what has worked for me in both Ubuntu and Windows. This is for python3. To do for python2, replace "3" with "2":

分享在 Ubuntu 和 Windows 中对我有用的东西。这是针对python3的。对于 python2,将“3”替换为“2”:

Ubuntu

Ubuntu

pip install virtualenv --user
virtualenv -p python3 /tmp/VIRTUAL
source /tmp/VIRTUAL/bin/activate
which python3

To install any package: pip install package

要安装任何软件包: pip install package

To get out of the virtual environment: deactivate

退出虚拟环境: deactivate

To activate again: source /tmp/VIRTUAL/bin/activate

再次激活: source /tmp/VIRTUAL/bin/activate

Full explanation here.

完整的解释在这里

Windows

视窗

(Assuming you have MiniConda installed and are in the Start Menu > Anaconda > Anaconda Terminal)

(假设您已安装 MiniConda 并且位于“开始”菜单 > Anaconda > Anaconda Terminal)

conda create -n VIRTUAL python=3  
activate VIRTUAL

To install any package: pip install packageor conda install package

安装任何软件包:pip install packageconda install package

To get out of the virtual environment: deactivate

退出虚拟环境: deactivate

To activate again: activate VIRTUAL

再次激活: activate VIRTUAL

Full explanation here.

完整的解释在这里

回答by jeremy.r

Sharing a personal case if it helps. It is that a virtual environment was previously arranged. Its path can be displayed by

如果有帮助,请分享个人案例。就是之前布置了一个虚拟环境。它的路径可以通过

echo $VIRTUAL_ENV

echo $VIRTUAL_ENV

Make sure that the it is writable to the current user. If not, using

确保它对当前用户是可写的。如果没有,使用

sudo ipython

sudo ipython

would certainly clear off the warning message.

肯定会清除警告消息。

In anaconda, if $VIRTUAL_ENV is independently arranged, one can simply delete this folder or rename it, and then restart the shell. Anaconda will recover to its default setup.

在anaconda中,如果单独设置$VIRTUAL_ENV,可以简单的删除这个文件夹或者重命名,然后重启shell。Anaconda 将恢复到其默认设置。