Python 如何使用 anaconda conda 命令安装 PyPi 包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29286624/
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-19 04:19:31  来源:igfitidea点击:

How to install PyPi packages using anaconda conda command

pythonpipanacondapypiconda

提问by user1507844

When using the Anacoda Python distribution, what is the best way to install a PyPi package that isn't available directly through Anaconda? For now I'm using:

使用 Anacoda Python 发行版时,安装无法直接通过 Anaconda 使用的 PyPi 包的最佳方法是什么?现在我正在使用:

conda pipbuild [pypi_name]
conda install --use-local [package_spec]

But I'm unclear if this is the best way and if conda update --allwill update these packages when updates are made available. I'm also unclear what the point of binstar is when PyPi already exists.

但我不清楚这是否是最好的方法,以及是否conda update --all会在更新可用时更新这些软件包。我也不清楚当 PyPi 已经存在时 binstar 的意义何在。

采纳答案by asmeurer

If you want to build conda packages for PyPI packages, the recommended way is to use conda skeleton pypi packageand use conda build packageon the recipe that it creates. To install the package, use conda install --use-local package(here and elsewhere, packageis the name of the PyPI package you wish to install).

如果你想为 PyPI 包构建 conda 包,推荐的方法是使用conda skeleton pypi package和使用conda build package它创建的配方。要安装该软件包,请使用conda install --use-local package(此处和其他地方,package是您要安装的 PyPI 软件包的名称)。

You will need to update the recipe each time the package is updated.

每次更新包时,您都需要更新配方。

You can also use pipto install these packages. There are two disadvantages: firstly, these packages won't be managed by conda at all. Secondly, these packages will not work if your default python version is different from the python version you are using in conda.

您也可以使用pip来安装这些软件包。有两个缺点:首先,这些包根本不会被 conda 管理。其次,如果您的默认 python 版本与您在 conda 中使用的 python 版本不同,这些包将不起作用。

回答by Chris Conlan

I will disagree with the accepted response and note that pip install [some-pypi-package]is often the best way to install PyPi packages in Conda environments.

我不同意接受的回应,并注意到这pip install [some-pypi-package]通常是在 Conda 环境中安装 PyPi 包的最佳方式。

While the packages won't be managed by the Conda package manager, they will still be managed by the Anaconda environment. It will download the correct version of the package for the active Python install and update it correctly using the pippackage manager.

虽然包不会由 Conda 包管理器管理,但它们仍将由 Anaconda 环境管理。它将为活动的 Python 安装下载正确版本的包,并使用pip包管理器正确更新它。

When using Anaconda, you should turn to condabefore pipwhen you can, but you don't lose any of the replicability benefits of using Anaconda when you use pip.

在使用 Anaconda 时,您应该尽可能conda早地转向pip,但是当您使用pip.

Anaconda recently published a doc that supports this: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#using-pip-in-an-environment

Anaconda 最近发布了一个支持这个的文档:https: //docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#using-pip-in-an-environment

回答by NichtJens

Since version 4.6.0, Conda has improved its interoperability with pip:

4.6.0 版本开始,Conda 改进了与 pip 的互操作性:

Conda and pip have historically had difficulties getting along. Pip hasn't respected Conda's environment constraints, while Conda has been all too happy to clobber pip-installed software. It's a mess. Conda 4.6.0 adds preview support for better interoperability. With this interoperability, Conda can use pip-installed packages to satisfy dependencies, and can even remove pip-installed software cleanly and replace them with Conda packages when appropriate. There's still room for improvement before pip and Conda are hunky-dory BFFs, but we hope this is a good start. This feature is disabled by default right now because it can significantly impact Conda's performance. If you'd like to try it, you can set this condarc setting:

Conda 和 pip 历来难以相处。Pip 不尊重 Conda 的环境限制,而 Conda 乐于破坏 pip 安装的软件。一团糟。Conda 4.6.0 添加了预览支持以实现更好的互操作性。通过这种互操作性,Conda 可以使用 pip 安装的包来满足依赖关系,甚至可以干净地删除 pip 安装的软件,并在适当的时候用 Conda 包替换它们。在 pip 和 Conda 成为笨拙的 BFF 之前仍有改进的空间,但我们希望这是一个好的开始。此功能现在默认禁用,因为它会显着影响 Conda 的性能。如果您想尝试一下,可以设置此 condarc 设置:

conda config --set pip_interop_enabled True

So, the way to get PyPI packages into conda (at the time of writing this) seems to be:

因此,将 PyPI 包放入 conda 的方法(在撰写本文时)似乎是:

pip install <package>

If you want conda to replace the PyPI packages with its own (where possible), just run:

如果您希望 conda 用自己的(在可能的情况下)替换 PyPI 包,只需运行:

conda update --all

Given that the above setting is made. Conda marks its own channels as higher priority than pip, thus packages will be replaced.

鉴于进行了上述设置。Conda 将自己的频道标记为比 pip 更高的优先级,因此包将被替换。