简体   繁体   English

conda和pip之间的区别在conda环境中安装

[英]Difference between conda and pip installs within a conda environment

I seem to be asking myself this question a lot, having recently switched to using conda environments (Anaconda), but I end up Googling and not getting too far. 我似乎很多问自己这个问题,最近转向使用conda环境(Anaconda),但我最终谷歌搜索并没有走得太远。

I now run all my projects within their own conda environments, as I like to keep everything as separate and with as little dependencies on other programs as possible. 我现在在他们自己的conda环境中运行我的所有项目,因为我希望将所有内容保持为独立,并尽可能少地依赖于其他程序。 For example, a recent environment: 例如,最近的环境:

conda create -n RL numpy tensorflow-gpu

Then I activate the environment, and realise "Oh - I forgot to install gym". 然后我激活环境,并意识到“哦 - 我忘了安装健身房”。 In this case, this is only available in the PIP package manager, and so I simply type pip install gym . 在这种情况下,这仅在PIP包管理器中可用,因此我只需键入pip install gym But in other cases, where the package exists within conda and pip, what is the best way to install it? 但在其他情况下,包中存在conda和pip,安装它的最佳方法是什么?

conda install package pip install package conda install package pip install package

Or in other words - what is the difference? 换句话说 - 有什么区别?

To provide the full picture, I'm running everything in Ubuntu 16.04, and switch between python 2 and 3 depending on the project. 为了提供完整的图片,我在Ubuntu 16.04中运行所有内容,并根据项目在python 2和3之间切换。 So some of my conda environments are in python 2, some are python 3. I've found that sometimes a pip3 install is required for python 3, but not always - why is this? 所以我的一些conda环境是在python 2中,有些是python 3.我发现python 3有时需要pip3 install ,但并不总是 - 为什么这样?

Secondly, my path links to the python setup in my Anaconda3 directory. 其次,我的路径链接到我的Anaconda3目录中的python设置。

My current idea is that if I install via conda , it installs directly to my environment, but via pip it installs to my anaconda3 site-packages, making it available to all conda environments under my Anaconda3 directory. 我目前的想法是,如果我通过conda安装,它会直接安装到我的环境,但通过pip安装到我的anaconda3站点包,使其可用于我的Anaconda3目录下的所有conda环境。 If this is the case, this means that if I pip install gym in one conda environment, it should also be available in all others - but this isn't the expected behaviour of environments as far as I am aware. 如果是这种情况,这意味着如果我在一个conda环境中pip install gym ,它也应该在所有其他环境中可用 - 但据我所知,这不是环境的预期行为。

Please feel free to correct my assumptions and knock some sense into me! 请随意纠正我的假设并对我有所了解!

For my understanding of Conda that, it manages for you all the dependencies. 对于我对Conda的理解,它为您管理所有依赖项。 For example if you have a package (like pandas) that requires another package (like numpy), it will download both (after warning you). 例如,如果你有一个需要另一个包(比如numpy)的包(比如pandas),它会同时下载(警告你之后)。

Where conda is becoming handy is that sometimes a specific package requires a specific version of another one (4.3 or later for example) and they can be conflicts between the packages. conda变得方便的地方有时特定包需要另一个特定版本(例如4.3或更高版本),它们可能是包之间的冲突。 The requirements and conflicts define a mathematical problem that can be solved thanks to a SAT solver. 需求和冲突定义了一个可以通过SAT求解器解决的数学问题。

You can find informations and link about that here: https://www.continuum.io/blog/developer/new-advances-conda-0 你可以在这里找到关于它的信息和链接: https//www.continuum.io/blog/developer/new-advances-conda-0

So each time you are installing a new package, it will upgrade (or sometimes downgrade if conflicts) other packages to ensure the functionning of each package. 因此,每次安装新软件包时,它都会升级(或者有时会在发生冲突时降级)其他软件包,以确保每个软件包的功能。 Personnaly, I go with conda and use pip only when the package is not managed by conda Personnaly,我使​​用conda并仅在包不是由conda管理时才使用pip

Another link if you are interested by conda: https://jakevdp.github.io/blog/2016/08/25/conda-myths-and-misconceptions/ 如果您对conda感兴趣,可以使用另一个链接: https ://jakevdp.github.io/blog/2016/08/25/conda-myths-and-misconceptions/

About pip3 , it is the naming used when you have both Python 2 and Python 3 installed, to avoid conflicts in the command. 关于pip3 ,它是安装Python 2和Python 3时使用的命名,以避免命令中的冲突。 In a python 3 environment, the command pip will be equivalent to pip3 . 在python 3环境中,命令pip将等同于pip3

For the behavior of pip , I can confirm that the installation is done only in the active environment and is not available to the other ones 对于pip的行为,我可以确认安装仅在活动环境中完成,并且不可用于其他环境

The difference is that conda will know about the new environment it created but pip will not. 区别在于conda会知道它创建的新环境,但pip不会。 You need to install pip inside the environment. 您需要在环境中安装pip。

If you create a new environment and activate it: eg 如果您创建一个新环境并将其激活:例如

conda create -n env_name
source activate env_name

Then install pip using conda: 然后使用conda安装pip:

conda install pip

(gotcha warning) if you run which pip this should give the path to the pip installation in the new conda environment (something like this): (有问题的警告)如果你运行which pip这应该在新的conda环境中提供pip安装的路径(类似这样):

/anaconda3/envs/env_name/bin/pip

however, just running pip install new_package still does not seem to work, you need to explicitly reference the full path (eg Tom Roth's blog post ) when installing pip packages inside a conda environment 然而,只是运行pip install new_package似乎仍然不起作用,你需要在conda环境中安装pip包时明确引用完整路径(例如Tom Roth的博客文章

/anaconda3/envs/env_name/bin/pip install new_package

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM