Python 如何在 Anaconda (Conda) 环境中跟踪 pip 安装的软件包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18640305/
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
How do I keep track of pip-installed packages in an Anaconda (Conda) environment?
提问by gromiczek
I've installed and have been using the AnacondaPython distribution, and I have started using the Anaconda (Conda) environment. I can use the standard conda install...
command to put packages from the distribution into my environments, but to use anything outside (i.e. Flask-WTF, flask-sqlalchemy, and alembic) I need to use pip install
in the active environment. However, when I look at the contents of the environment, either in the directory, or using conda list
these pip install
ed packages don't show up.
我已经安装并一直在使用AnacondaPython 发行版,并且我已经开始使用 Anaconda (Conda) 环境。我可以使用标准conda install...
命令将分发包中的包放入我的环境中,但要使用外部的任何东西(即 Flask-WTF、flask-sqlalchemy 和 alembic),我需要pip install
在活动环境中使用。但是,当我查看环境的内容时,无论是在目录中,还是使用conda list
这些pip install
ed 包都没有显示出来。
Using pip freeze
and pip list
just lists every package I've ever installed.
使用pip freeze
并pip list
只列出我安装过的每个包。
Is there a way to keep track of what is in each of my Anaconda env
s (both pip
and conda
installed)?
有没有办法跟踪我的每个 Anaconda 中env
的内容(包括pip
和conda
已安装的)?
采纳答案by conner.xyz
conda-envnow does this automatically (if pip was installed with conda).
conda-env现在自动执行此操作(如果 pip 与 conda 一起安装)。
You can see how this works by using the export tool used for migrating an environment:
您可以使用用于迁移环境的导出工具来了解其工作原理:
conda env export -n <env-name> > environment.yml
The file will list both conda packages and pip packages:
该文件将列出 conda 包和 pip 包:
name: stats
channels:
- javascript
dependencies:
- python=3.4
- bokeh=0.9.2
- numpy=1.9.*
- nodejs=0.10.*
- flask
- pip:
- Flask-Testing
If you're looking to follow through with exporting the environment, move environment.yml
to the new host machine and run:
如果您希望继续导出环境,请移至environment.yml
新主机并运行:
conda env create -f path/to/environment.yml
回答by Viktor Kerkez
conda
will only keep track of the packages it installed. And pip
will give you the packages that were either installed using the pip
installer itself or they used setuptools
in their setup.py
so conda build generated the egg information. So you have basically three options.
conda
只会跟踪它安装的软件包。而且pip
会给你使用的是要么安装的软件包pip
安装程序本身或者他们用setuptools
他们setup.py
产生卵子的信息,以便畅达的构建。所以你基本上有三个选择。
You can take the union of the
conda list
andpip freeze
and manage packages that were installed usingconda
(that show in theconda list
) with theconda
package manager and the ones that are installed withpip
(that show inpip freeze
but not inconda list
) withpip
.Install in your environment only the
python
,pip
anddistribute
packages and manage everything withpip
. (This is not that trivial if you're on Windows...)Build your own
conda
packages, and manage everything withconda
.
您可以将
conda list
和pip freeze
并管理使用conda
(显示在 中conda list
)与conda
包管理器安装的包以及使用 安装的包pip
(显示在 中pip freeze
但不在 中conda list
)与pip
。仅在您的环境中安装
python
,pip
并distribute
使用pip
. (如果你在 Windows 上,这不是那么简单......)构建您自己的
conda
包,并使用conda
.
I would personally recommend the third option since it's very easy to build conda
packages. There is a git repository of example recipes on the continuum's github account. But it usually boils down to:
我个人会推荐第三个选项,因为它很容易构建conda
包。在 continuum 的 github 帐户上有一个示例食谱的 git 存储库。但它通常归结为:
conda skeleton pypi PACKAGE
conda build PACKAGE
or just:
要不就:
conda pipbuild PACKAGE
Also when you have built them once, you can upload them to https://binstar.org/and just install from there.
此外,当您构建它们一次后,您可以将它们上传到https://binstar.org/并从那里安装。
Then you'll have everything managed using conda
.
然后,您将使用conda
.
回答by Travis Oliphant
There is a branch of conda (new-pypi-install) that adds better integration with pip and PyPI. In particular conda list will also show pip installed packages and conda install will first try to find a conda package and failing that will use pip to install the package.
conda (new-pypi-install) 的一个分支增加了与 pip 和 PyPI 的更好集成。特别是 conda list 还将显示 pip 安装的包,conda install 将首先尝试查找 conda 包,如果失败,将使用 pip 安装包。
This branch is scheduled to be merged later this week so that version 2.1 of conda will have better pip-integration with conda.
该分支计划在本周晚些时候合并,以便 conda 2.1 版与 conda 有更好的 pip 集成。
回答by Josh Hemann
I followed @Viktor Kerkez's answer and have had mixed success. I found that sometimes this recipe of
我遵循了@Viktor Kerkez 的回答,并取得了不同的成功。我发现有时这个食谱
conda skeleton pypi PACKAGE
conda build PACKAGE
conda 骨架 pypi 包
conda 构建包
would look like everything worked but I could not successfully import PACKAGE. Recently I asked about this on the Anaconda user groupand heard from @Travis Oliphant himself on the best way to use conda to build and manage packages that do not ship with Anaconda. You can read this thread here, but I'll describe the approach below to hopefully make the answers to the OP's question more complete...
看起来一切正常,但我无法成功导入 PACKAGE。最近我在Anaconda 用户组上询问了这个问题,并从 @Travis Oliphant 本人那里听到了使用 conda 构建和管理不随 Anaconda 提供的软件包的最佳方法。您可以在此处阅读此主题,但我将在下面描述方法,以期使 OP 问题的答案更加完整...
Example: I am going to install the excellent prettyplotlib packageon Windows using conda 2.2.5.
示例:我将使用 conda 2.2.5 在 Windows 上安装优秀的prettyplotlib 包。
1a)conda build --build-recipe prettyplotlib
1a)conda build --build-recipe prettyplotlib
You'll see the build messages all look good until the final TEST section of the build. I saw this error
在构建的最后 TEST 部分之前,您会看到构建消息看起来都很好。我看到了这个错误
File "C:\Anaconda\conda-bld\test-tmp_dir\run_test.py", line 23 import None SyntaxError: cannot assign to None TESTS FAILED: prettyplotlib-0.1.3-py27_0
文件“C:\Anaconda\conda-bld\test-tmp_dir\run_test.py”,第 23 行导入无语法错误:无法分配给无测试失败:prettyplotlib-0.1.3-py27_0
1b)Go into /conda-recipes/prettyplotlib and edit the meta.yaml file. Presently, the packages being set up like in step 1a result in yaml files that have an error in the test
section. For example, here is how mine looked for prettyplotlib
1b)进入 /conda-recipes/prettyplotlib 并编辑 meta.yaml 文件。目前,在步骤 1a 中设置的包会导致 yaml 文件在该test
部分中有错误。例如,这是我的查找方式prettyplotlib
test: # Python imports imports:
-
- prettyplotlib
- prettyplotlib
Edit this section to remove the blank line preceded by the - and also remove the redundant prettyplotlib line. At the time of this writing I have found that I need to edit most meta.yaml files like this for external packages I am installing with conda, meaning that there is a blank import line causing the error along with a redundant import of the given package.
编辑此部分以删除 - 前面的空行,并删除多余的 prettyplotlib 行。在撰写本文时,我发现我需要为使用 conda 安装的外部包编辑大多数像这样的 meta.yaml 文件,这意味着有一个空白的导入行导致错误以及给定包的冗余导入.
1c)Rerun the command from 1a, which should complete with out error this time. At the end of the build you'll be asked if you want to upload the build to binstar. I entered No and then saw this message:
1c)重新运行 1a 中的命令,这次应该没有错误地完成。在构建结束时,系统会询问您是否要将构建上传到binstar。我输入 No 然后看到了这条消息:
If you want to upload this package to binstar.org later, type:
$ binstar upload C:\Anaconda\conda-bld\win-64\prettyplotlib-0.1.3-py27_0.tar.bz2
如果您想稍后将此包上传到 binstar.org,请键入:
$ binstar 上传 C:\Anaconda\conda-bld\win-64\prettyplotlib-0.1.3-py27_0.tar.bz2
That tar.bz2 file is the build that you now need to actually install.
该 tar.bz2 文件是您现在需要实际安装的构建。
2)conda install C:\Anaconda\conda-bld\win-64\prettyplotlib-0.1.3-py27_0.tar.bz2
2)conda install C:\Anaconda\conda-bld\win-64\prettyplotlib-0.1.3-py27_0.tar.bz2
Following these steps I have successfully used conda to install a number of packages that do not come with Anaconda. Previously, I had installed some of these using pip, so I did pip uninstall PACKAGE
prior to installing PACKAGE with conda. Using conda, I can now manage (almost) all of my packages with a single approach rather than having a mix of stuff installed with conda, pip, easy_install, and python setup.py install.
按照这些步骤,我已经成功地使用 conda 安装了许多 Anaconda 没有的软件包。以前,我使用 pip 安装了其中的一些,所以我pip uninstall PACKAGE
在使用 conda 安装 PACKAGE 之前这样做了。使用 conda,我现在可以用一种方法管理(几乎)我所有的包,而不是使用 conda、pip、easy_install 和 python setup.py install 安装混合的东西。
For context, I think this recent blog postby @Travis Oliphant will be helpful for people like me who do not appreciate everything that goes into robust Python packaging but certainly appreciate when stuff "just works". conda seems like a great way forward...
就上下文而言,我认为@Travis Oliphant最近发表的这篇博文将有助于像我这样不喜欢强大的 Python 包装中的所有内容但肯定会喜欢“正常工作”的人。conda 似乎是一个很好的前进方向......
回答by Wes Turner
conda env export
lists all conda and pip packages in an environment. conda-env
must be installed in the conda root (conda install -c conda conda-env
).
conda env export
列出环境中的所有 conda 和 pip 包。conda-env
必须安装在 conda 根 ( conda install -c conda conda-env
) 中。
To write an environment.yml
file describing the current environment:
要编写environment.yml
描述当前环境的文件:
conda env export > environment.yml
References:
参考:
回答by Gianluca
I usually prefix the 'bin/pip' folder for the specific environment you want to install the package before the 'pip' command. For instance, if you would like to install pymc3 in the environment py34, you should use this command:
我通常在“pip”命令之前为要安装包的特定环境添加“bin/pip”文件夹的前缀。例如,如果你想在 py34 环境中安装 pymc3,你应该使用这个命令:
~/anaconda/envs/py34/bin/pip install git+https://github.com/pymc-devs/pymc3
You basically just need to find the right path to your environment 'bin/pip' folder and put it before the install command.
您基本上只需要找到环境“bin/pip”文件夹的正确路径并将其放在安装命令之前。
回答by Chris Withers
This is why I wrote Picky: http://picky.readthedocs.io/
这就是我写 Picky 的原因:http: //picky.readthedocs.io/
It's a python package that tracks packages installed with either pip or conda in either virtualenvs and conda envs.
它是一个 python 包,用于跟踪在 virtualenvs 和 conda envs 中使用 pip 或 conda 安装的包。
回答by kmario23
My which pip
shows the following path:
我的which pip
显示以下路径:
$ which pip
/home/kmario23/anaconda3/bin/pip
So, whatever package I install using pip install <package-name>
will have to be reflected in the list of packages when the list is exported using:
因此,pip install <package-name>
当使用以下命令导出列表时,我安装的任何包都必须反映在包列表中:
$ conda list --export > conda_list.txt
But, I don't. So, instead I used the following command as suggested by several others:
但是,我没有。因此,我按照其他几个人的建议使用了以下命令:
# get environment name by
$ conda-env list
# get list of all installed packages by (conda, pip, etc.,)
$ conda-env export -n <my-environment-name> > all_packages.yml
# if you haven't created any specific env, then just use 'root'
Now, I can see all the packages in my all-packages.yml
file.
现在,我可以看到我的all-packages.yml
文件中的所有包。
回答by M.H.
I think what's missing here is that when you do:
我认为这里缺少的是当你这样做时:
>pip install .
to install a local package with a setup.py, it installs a package that is visible to allthe conda envs that use the same versionof python. Note I am using the conda version of pip!
要使用 setup.py 安装本地包,它会安装一个对所有使用相同版本python 的conda 环境可见的包。注意我使用的是 conda 版本的 pip!
e.g., if I'm using python2.7 it puts the local package here:
例如,如果我使用的是 python2.7,它会将本地包放在这里:
/usr/local/anaconda/lib/python2.7/site-packages
/usr/local/anaconda/lib/python2.7/site-packages
If I then later create a new conda env with python=2.7 (= the default):
如果我稍后使用 python=2.7(= 默认值)创建一个新的 conda env:
>conda create --name new
>source activate new
And then do:
然后做:
(new)>conda list // empty - conda is not aware of any packages yet
However, if I do:
但是,如果我这样做:
(new)>pip list // the local package installed above is present
So in this case, conda does not know about the pip package, but the package is available to python.
所以在这种情况下,conda 不知道 pip 包,但该包对 python 可用。
However, If I instead install the local package (again using pip) afterI've created (and activated) the new conda env, nowconda sees it:
但是,如果我在创建(并激活)新的 conda env之后安装本地包(再次使用 pip),现在conda 会看到它:
(new)>conda list // sees that the package is there and was installed by pip
So I think the interaction between conda and pip has some issues - ie, using pip to install a local package from within one conda env makes that package available (but not seen via conda list) to all other conda envs of the same python version.
因此,我认为 conda 和 pip 之间的交互存在一些问题 - 即,使用 pip 从一个 conda env 中安装本地包会使该包可用于相同 python 版本的所有其他 conda env(但无法通过 conda 列表看到)。
回答by Kavish
You can start by installing the below given command in the conda environment:
您可以首先在 conda 环境中安装以下给定命令:
conda install pip
conda install pip
Followed by installing all pip packages you need in the environment.
接下来是在环境中安装您需要的所有pip包。
After installing all the conda and pip packages to export the environment use:
安装所有 conda 和 pip 包以导出环境后使用:
conda env export -n <env-name> > environment.yml
conda env export -n <env-name> > environment.yml
This will create the required file in the folder
这将在文件夹中创建所需的文件