Python 使用 Pip 将软件包安装到 Anaconda 环境
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41060382/
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
Using Pip to install packages to Anaconda Environment
提问by jeffery_the_wind
conda 4.2.13 MacOSX 10.12.1
康达 4.2.13 MacOSX 10.12.1
I am trying to install packages from pip
to a fresh environment (virtual) created using anaconda. In the Anaconda docsit says this is perfectly fine. It is done the same way as for virtualenv.
我正在尝试将软件包安装pip
到使用 anaconda 创建的新环境(虚拟)中。 在 Anaconda 文档中,它说这完全没问题。它的完成方式与 virtualenv 相同。
Activate the environment where you want to put the program, then pip install a program...
激活要放置程序的环境,然后pip install一个程序...
I created an empty environment in Ananconda like this:
我在 Ananconda 中创建了一个空环境,如下所示:
conda create -n shrink_venv
Activate it:
激活它:
source activate shrink_venv
I then can see in the terminal that I am working in my env (shrink_venv)
. Problem is coming up, when I try to install a package using pip
:
然后我可以在终端中看到我正在我的 env 中工作(shrink_venv)
。问题来了,当我尝试使用pip
以下方法安装软件包时:
(shrink_venv): pip install Pillow
Requirement already satisfied (use --upgrade to upgrade): Pillow in /Library/Python/2.7/site-packages
So I can see it thinks the requirement is satisfied from the system-wide package. So it seems the environment is not working correctly, definitely not like it said in the docs. Am I doing something wrong here?
所以我可以看到它认为系统范围的包满足了要求。所以看起来环境不能正常工作,绝对不像文档中所说的那样。我在这里做错了吗?
Just a note, I know you can use conda install
for the packages, but I have had an issue with Pillow from anaconda, so I wanted to get it from pip
, and since the docs say that is fine.
请注意,我知道您可以使用conda install
这些软件包,但是我遇到了来自 anaconda 的 Pillow 的问题,所以我想从 中获取它pip
,因为文档说这很好。
Output of which -a pip
:
的输出which -a pip
:
/usr/local/bin/pip
/Users/my_user/anaconda/bin/pip
** UPDATE **
I see this is pretty common issue. What I have found is that the conda env doesn't play well with the PYTHONPATH. The system seems to always look in the PYTHONPATH locations even when you're using a conda environment. Now, I always run unset PYTHONPATH
when using a conda environment, and it works much better. I'm on a mac.
** 更新 ** 我认为这是很常见的问题。我发现 conda env 不能很好地与 PYTHONPATH 配合使用。即使您使用的是 conda 环境,系统似乎总是在 PYTHONPATH 位置中查找。现在,我总是unset PYTHONPATH
在使用 conda 环境时运行,并且效果更好。我在 mac 上。
回答by Windmill
For others who run into this situation, I found this to be the most straightforward solution:
对于遇到这种情况的其他人,我发现这是最直接的解决方案:
Run
conda create -n venv_name
andsource activate venv_name
, wherevenv_name
is the name of your virtual environment.Run
conda install pip
. This will install pip to your venv directory.Find your anaconda directory, and find the actual venv folder. It should be somewhere like
/anaconda/envs/venv_name/
.Install new packages by doing
/anaconda/envs/venv_name/bin/pip install package_name
.
运行
conda create -n venv_name
和source activate venv_name
,venv_name
您的虚拟环境的名称在哪里。运行
conda install pip
。这会将 pip 安装到您的 venv 目录。找到您的 anaconda 目录,并找到实际的 venv 文件夹。它应该像
/anaconda/envs/venv_name/
.通过执行
/anaconda/envs/venv_name/bin/pip install package_name
.
This should now successfully install packages using that virtual environment's pip!
现在应该使用该虚拟环境的 pip 成功安装软件包!
回答by sambeth
All you have to do is open Anaconda Prompt and type
您所要做的就是打开 Anaconda Prompt 并输入
pip install package-name
It will automatically install to the anaconda environment without having to use
会自动安装到anaconda环境,无需使用
conda install package-name
Since some of the conda packages may lack support overtime it is required to install using pip and this is one way to do it
由于某些 conda 软件包可能缺乏超时支持,因此需要使用 pip 进行安装,这是一种方法
If you have pip installed in anaconda you can run the following in jupyter notebook or in your python shell that is linked to anaconda
如果您在 anaconda 中安装了 pip,则可以在 jupyter notebook 或链接到 anaconda 的 python shell 中运行以下命令
pip.main(['install', 'package-name'])
Check your version of pip with pip.__version__
. If it is version 10.x.x
or above, then install your python package with this line of code
检查您的 pip 版本pip.__version__
。如果是版本10.x.x
或更高版本,则使用这行代码安装您的python包
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'package-name'])
In your jupyter notebook, you can install python packages through pip in a cell this way;
在您的 jupyter notebook 中,您可以通过这种方式在单元格中通过 pip 安装 python 包;
!pip install package-name
or you could use your python version associated with anaconda
或者您可以使用与 anaconda 关联的 python 版本
!python3.6 -m pip install package-name
回答by alex_danielssen
I solved this problem the following way:
我通过以下方式解决了这个问题:
If you have a non-conda pip as your default pip but conda python is your default python (as below)
如果你有一个非 conda pip 作为你的默认 pip 但 conda python 是你的默认 python (如下)
>which -a pip
/home/<user>/.local/bin/pip
/home/<user>/.conda/envs/newenv/bin/pip
/usr/bin/pip
>which -a python
/home/<user>/.conda/envs/newenv/bin/python
/usr/bin/python
Then instead of just calling
pip install <package>
, you can use the module flag -m
with python so that it uses the anaconda python for the installation
然后,而不是仅仅调用
pip install <package>
,您可以将模块标志-m
与 python 一起使用,以便它使用 anaconda python 进行安装
python -m pip install <package>
This installs the package to the anaconda library directory rather than to the library directory associated with (the non-anaconda) pip
这会将包安装到 anaconda 库目录而不是与(非 anaconda)pip 关联的库目录
回答by Allen Ye
If you didn't add pip when creating conda environment
如果在创建 conda 环境时没有添加 pip
conda create -n env_name pip
and also didn't install pip inside the environment
并且也没有在环境中安装 pip
source activate env_name
conda install pip
then the only pip you got is the system pip, which will install packages globally.
那么你得到的唯一 pip 就是系统 pip,它将全局安装包。
Bus as you can see in this issue, even if you did either of the procedure mentioned above, the behavior of pip inside conda environment is still kind of undefined.
正如您在本期中看到的 Bus ,即使您执行了上述任何一个过程,conda 环境中的 pip 行为仍然是未定义的。
To ensure using the pip installed inside conda environment without having to type the lengthy /home/username/anaconda/envs/env_name/bin/pip
, I wrote a shell function:
为了确保使用安装在 conda 环境中的 pip 而不必输入冗长的/home/username/anaconda/envs/env_name/bin/pip
,我编写了一个 shell 函数:
# Using pip to install packages inside conda environments.
cpip() {
ERROR_MSG="Not in a conda environment."
ERROR_MSG="$ERROR_MSG\nUse \`source activate ENV\`"
ERROR_MSG="$ERROR_MSG to enter a conda environment."
[ -z "$CONDA_DEFAULT_ENV" ] && echo "$ERROR_MSG" && return 1
ERROR_MSG='Pip not installed in current conda environment.'
ERROR_MSG="$ERROR_MSG\nUse \`conda install pip\`"
ERROR_MSG="$ERROR_MSG to install pip in current conda environment."
[ -e "$CONDA_PREFIX/bin/pip" ] || (echo "$ERROR_MSG" && return 2)
PIP="$CONDA_PREFIX/bin/pip"
"$PIP" "$@"
}
Hope this is helpful to you.
希望这对你有帮助。
回答by modatamoproblems
This is what worked for me (Refer to image linked)
这对我有用(参考图片链接)
- Open Anaconda
- Select Environments in the left hand pane below home
- Just to the right of where you selected and below the "search environments" bar, you should see base(root). Click on it
- A triangle pointing right should appear, click on it an select "open terminal"
- Use the regular pip install command here. There is no need to point to an environment/ path
- 打开蟒蛇
- 在主页下方的左侧窗格中选择环境
- 在您选择的位置右侧和“搜索环境”栏下方,您应该看到 base(root)。点击它
- 应该会出现一个指向右侧的三角形,单击它并选择“打开终端”
- 在此处使用常规 pip install 命令。无需指向环境/路径
For future reference, you can find the folder your packages are downloading to if you happen to have a requirement already satisfied. You can see it if you scroll up in the terminal. It should read something like: requirement already satisfied and then the path
为了将来参考,如果您碰巧已经满足了某个要求,您可以找到您的软件包下载到的文件夹。如果您在终端中向上滚动,您可以看到它。它应该是这样的:需求已经满足,然后是路径
[]
[ ]
回答by intsco
python -m pip install Pillow
python -m pip install Pillow
Will use pip of current Python activated with
将使用激活的当前 Python 的 pip
source activate shrink_venv
source activate shrink_venv
回答by BorjaEst
Depends on how did you configure your PATH environmental variable. When your shell resolves the call to pip, which is the first bin it will find?
取决于您如何配置 PATH 环境变量。当您的 shell 解析对 pip 的调用时,它将找到的第一个 bin 是哪个?
(test)$ whereis pip
pip: /home/borja/anaconda3/envs/test/bin/pip /home/borja/anaconda3/bin/pip
Make sure the bin folder from your anaconda installation is before /usr/lib (depending on how you did install pip). So an example:
确保 anaconda 安装中的 bin 文件夹位于 /usr/lib 之前(取决于您安装 pip 的方式)。所以一个例子:
(test) borja@xxxx:~$ pip install djangorestframework
....
Successfully installed asgiref-3.2.3 django-3.0.3 djangorestframework-3.11.0 pytz-2019.3 sqlparse-0.3.1
(test) borja@xxxx:~$ conda list | grep django
django 3.0.3 pypi_0 pypi
djangorestframework 3.11.0 pypi_0 pypi
We can see the djangorestframework was installed in my test environment but if I check my base:
我们可以看到 djangorestframework 安装在我的测试环境中,但是如果我检查我的基础:
(base) borja@xxxx:~$ conda list | grep django
It is empty.
它是空的。
Personally I like to handle all my PATH configuration using .pam_environment, here an example:
我个人喜欢使用.pam_environment处理我所有的 PATH 配置,这里有一个例子:
(base) borja@xxxx:~$ cat .pam_environment
PATH DEFAULT=/home/@{PAM_USER}/anaconda3/bin:${PATH}
One extra commet. The way how you install pip might create issues:
一颗额外的彗星。您安装 pip 的方式可能会产生问题:
You should use: conda install pip --> new packages installed with pip will be added to conda list.
You shodul NOTuse: sudo apt install python3-pip --> new packages will not be added to conda list (so are not managed by conda) but you will still be able to use them (chance of conflict).
您应该使用: conda install pip --> 使用 pip 安装的新软件包将添加到 conda 列表中。
您不应该使用: sudo apt install python3-pip --> 新软件包不会添加到 conda 列表中(因此不受 conda 管理),但您仍然可以使用它们(可能发生冲突)。
回答by Little Bobby Tables
For those wishing to install a small number of packages in conda with pip then using,
对于那些希望使用 pip 在 conda 中安装少量软件包然后使用的人,
sudo $(which pip) install <instert_package_name>
worked for me.
为我工作。
Explainaton
说明
It seems, for me anyway, that which pip
is very reliable for finding the conda env pip path to where you are. However, when using sudo
, this seems to redirect paths or otherwise break this.
无论如何,对我来说,这似乎which pip
非常可靠地找到了你所在位置的 conda env pip 路径。但是,在使用时sudo
,这似乎会重定向路径或以其他方式破坏它。
Using the $(which pip)
executes this independently of the sudo
or any of the commands and is akin to running /home/<username>/(mini)conda(3)/envs/<env_name>/pip
in Linux. This is because $()
is run separately and the text output added to the outer command.
使用$(which pip)
独立sudo
于命令或任何命令执行此操作,类似于/home/<username>/(mini)conda(3)/envs/<env_name>/pip
在 Linux 中运行。这是因为$()
单独运行并且文本输出添加到外部命令。
回答by Prabhakar Prasad
All above answers are mainly based on use of virtualenv. I just have fresh installation of anaconda3 and don't have any virtualenv installed in it. So, I have found a better alternative to it without wondering about creating virtualenv.
以上所有答案主要基于virtualenv的使用。我只是全新安装了 anaconda3,并且没有安装任何 virtualenv。所以,我找到了一个更好的替代方案,而无需考虑创建 virtualenv。
If you have many pip and python version installed in linux, then first run below command to list all installed pip paths.
如果你在 linux 中安装了很多 pip 和 python 版本,那么首先运行下面的命令来列出所有安装的 pip 路径。
whereis pip
点子在哪里
You will get something like this as output.
你会得到类似这样的输出。
pip: /usr/bin/pip
/home/prabhakar/anaconda3/bin/pip
/usr/share/man/man1/pip.1.gz
pip: /usr/bin/pip
/home/prabhakar/anaconda3/bin/pip
/usr/share/man/man1/pip.1.gz
Copy the path of pip which you want to use to install your package and paste it after sudo replacing /home/prabhakar/anaconda3/bin/pip
in below command.
复制要用于安装包的 pip 路径,并/home/prabhakar/anaconda3/bin/pip
在以下命令中替换 sudo 后粘贴它。
sudo
/home/prabhakar/anaconda3/bin/pip
install<package-name>
须藤
/home/prabhakar/anaconda3/bin/pip
安装<package-name>
This worked pretty well for me. If you have any problem installing, please comment.
这对我来说效果很好。如果安装有问题,请留言。
回答by Code_Worm
if you're using windows OS open Anaconda Promptand type activate yourenvname
如果您使用的是 Windows 操作系统,请打开Anaconda Prompt并输入activate yourenvname
And if you're using mac or Linux OS open Terminaland type source activate yourenvname
如果您使用的是 mac 或 Linux 操作系统,请打开终端并输入source activate yourenvname
yourenvname
here is your desired environment in which you want to install pip package
yourenvname
这是您想要安装 pip 包的所需环境
after typing above command you must see that your environment name is changed from base
to your typed environment yourenvname
in console output (which means you're now in your desired environment context)
键入以上命令后,您必须看到您的环境名称从控制台输出中的base
键入环境更改为您键入的环境yourenvname
(这意味着您现在处于所需的环境上下文中)
Then all you need to do is normal pip install
command e.g pip install yourpackage
然后你需要做的就是正常的pip install
命令,例如pip install yourpackage
By doing so, the pip package will be installed in your Conda environment
通过这样做,pip 包将安装在您的 Conda 环境中