简体   繁体   English

使用conda-forge包替换pip包的简便方法

[英]Easy way to replace pip packages with conda-forge packages

I have been using anaconda 64-bit python v3.6 on Windows 10 for quite some time but only recently discovered conda-forge. 我已经在Windows 10上使用anaconda 64位python v3.6已经有一段时间了,但最近才发现了conda-forge。 I discovered there are many python packages which I installed using pip are actually available on conda-forge. 我发现使用pip安装的许多python包实际上都可以在conda-forge上使用。

I would like to replace the pip packages with conda-forge packages. 我想用conda-forge包替换pip包。 I have been doing this manually but it is too tedious because they are many pip packages. 我一直在手动这样做,但它太繁琐,因为它们是很多pip包。 Is there an easy way to replace the pip packages with conda-forge packages automatically with a single command line? 有一种简单的方法可以使用一个命令行自动用conda-forge软件包替换pip软件包吗? I am open to using any other convenient method instead of doing it manually. 我愿意使用任何其他方便的方法而不是手动操作。

There is no idiomatic way to approach this issue other than some bash hackery: 除了一些bash hackery之外,没有惯用的方法可以解决这个问题:

An issue is that conda's repository may not be as comprehensive, and does not cover all versions. 问题是conda的存储库可能不够全面,并不涵盖所有版​​本。 Assuming all versions will work, here is an easy script: 假设所有版本都可以使用,这是一个简单的脚本:

conda install $(pip freeze | sed 's/==/=/g')

Because pip requires == to pin the version meanwhile conda requires = , simply sed the input to be in the correct format. 由于pip需要==来同时引脚版本conda需要= ,只需sed输入是正确的格式。

If it doesn't work, then you have to go for the riskier method: 如果它不起作用,那么你必须采用更危险的方法:

(sandbox) ❯ for pkg in $(pip freeze | sed 's/==/=/g'); do conda install -y $pkg; done

I even made a sandbox conda environment in case I screw up my python. 我甚至做了一个沙盒conda环境,以防我搞砸了我的python。 This will install anything you have in pip , and if it fails(ie corresponding version doesnt exist in conda repos), then it silently goes on to install the next package. 这将安装你在pip任何东西,如果它失败(即conda repos中不存在相应的版本),那么它会默默地继续安装下一个包。

Note that this is highly risky and you should definitely check what you installed and what you didn't afterwards. 请注意,这是非常危险的,您应该检查您安装的内容以及之后没有安装的内容。

First, I recommend you to create environment and install libraries to it. 首先,我建议您创建环境并为其安装库。 This way, your "base" environment stays clean and you can come back to it anytime. 这样,您的“基础”环境保持干净,您可以随时回来。 If you do so, then the fix is easy: delete the environment with many pip libraries and create a new environment to start over. 如果您这样做,那么修复很容易:删除具有许多pip库的环境并创建一个新环境以重新开始。 Conda environment is explained here https://conda.io/docs/user-guide/tasks/manage-environments.html . 这里解释了Conda环境https://conda.io/docs/user-guide/tasks/manage-environments.html

That said, If you would like to automate replacing the installed pip libraries by the one from conda-forge, you probably can do so using the command as below. 也就是说,如果你想用conda-forge自动替换已安装的pip库,你可以使用下面的命令来实现。 I experimented on bash terminal, but I guess similar can be done on the Anaconda prompt or command prompt. 我在bash终端上进行了实验,但我想可以在Anaconda提示或命令提示符上进行类似的操作。

But doing this kind of thing by command has a risk of destroying your environment. 但是按命令执行此类操作可能会破坏您的环境。 To keep your current environment just in case, you should save the information by: 为了保持您当前的环境以防万一,您应该通过以下方式保存信息:

conda env export > environment.yml

For the experiment, create a test environment where only pip is installed. 对于实验,创建一个仅安装pip的测试环境。

conda create -y -n testenv pip
source activate testenv

Then install two libraries via pip, tqdm (available on conda-forge ) and janome (not available on conda cloud). 然后通过pip, tqdm (在tqdm conda-forge tqdm可用)和janome (在conda云上不可用)安装两个库。

pip install tqdm janome

My environment now look like below. 我的环境现在如下所示。

conda list

## Name                    Version                   Build  Channel
ca-certificates           2018.03.07                    0  
certifi                   2018.8.13                py37_0  
Janome                    0.3.6                     <pip>
libedit                   3.1.20170329         h6b74fdf_2  
libffi                    3.2.1                hd88cf55_4  
libgcc-ng                 8.2.0                hdf63c60_1  
libstdcxx-ng              8.2.0                hdf63c60_1  
ncurses                   6.1                  hf484d3e_0  
openssl                   1.0.2p               h14c3975_0  
pip                       10.0.1                   py37_0  
python                    3.7.0                hc3d631a_0  
readline                  7.0                  ha6073c6_4  
setuptools                40.0.0                   py37_0  
sqlite                    3.24.0               h84994c4_0  
tk                        8.6.7                hc745277_3  
tqdm                      4.25.0                    <pip>
wheel                     0.31.1                   py37_0  
xz                        5.2.4                h14c3975_4  
zlib                      1.2.11               ha838bed_2  

Now, we want to do the following: For each library installed via pip , if it is on conda-forge , install it and uninstall the one from pip . 现在,我们要执行以下操作:对于通过pip安装的每个库,如果它在conda-forge ,请安装它并从pip卸载。

Here is a command to do so... 这是一个命令...

for lib in `conda list | grep '<pip>' | cut -f 1 -d ' '`; \
do 
  echo "*****checking $lib*****"; \
  conda install -y -c conda-forge $lib && \
  pip uninstall -y $lib; \
done
  • Line 1: collect libraries you installed via pip. 第1行:收集您通过pip安装的库。
  • Line 4: install the same library from conda-forge. 第4行:从conda-forge安装相同的库。
  • Line 5: if installed from conda-forge, uninstall the pip one. 第5行:如果从conda-forge安装,请卸载pip one。

Now my environment looks like below. 现在我的环境如下所示。

conda list

# Name                    Version                   Build  Channel
ca-certificates           2018.8.13            ha4d7672_0    conda-forge
certifi                   2018.4.16                py37_0    conda-forge
Janome                    0.3.6                     <pip>
libedit                   3.1.20170329         h6b74fdf_2  
libffi                    3.2.1                hd88cf55_4  
libgcc-ng                 8.2.0                hdf63c60_1  
libstdcxx-ng              8.2.0                hdf63c60_1  
ncurses                   6.1                  hf484d3e_0  
openssl                   1.0.2o               h470a237_1    conda-forge
pip                       10.0.1                   py37_0  
python                    3.7.0                hc3d631a_0  
readline                  7.0                  ha6073c6_4  
setuptools                40.0.0                   py37_0  
sqlite                    3.24.0               h84994c4_0  
tk                        8.6.7                hc745277_3  
tqdm                      4.24.0                     py_1    conda-forge
wheel                     0.31.1                   py37_0  
xz                        5.2.4                h14c3975_4  
zlib                      1.2.11               ha838bed_2  

暂无
暂无

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

相关问题 除非必要,否则如何避免使用 conda-forge 包? - How to avoid using conda-forge packages unless necessary? 将Conda更新到4.6.1以集成PowerShell后,Conda无法安装Conda-forge软件包 - Conda Not Able to Install Conda-forge Packages After Updating Conda to 4.6.1 to Integrate PowerShell PackagesNotFoundError:以下软件包在当前频道中不可用,添加 conda-forge 频道后? - PackagesNotFoundError: The following packages are not available from current channels, AFTER adding conda-forge channel? 在仍然安装 MKL 版本的软件包的同时,conda-forge 是否可以优先于默认值? - Can conda-forge have priority over defaults while still installing MKL versions of packages? 使用conda-forge强制升级软件包时有破坏anaconda python的风险吗? - Any risk of breaking anaconda python when force upgrading packages using conda-forge? 使用诗歌从 conda-forge(例如 cartopy)安装预构建包,而不依赖 conda(仅使用通道) - Install prebuilt packages from conda-forge (e.g. cartopy) using poetry without relying on conda (using only the channel) 将 Pip 包传输到 conda - Transfer Pip packages to conda 自动安装 conda-forge 和 pip python 库的混合 - Automating install of a mix of conda-forge and pip python libraries 安装 conda 包时出错:conda.core.link:_execute(696):安装包“conda-forge::qt-5.9.7-h506e8af_3”时出错 - Error installing conda packages: conda.core.link:_execute(696): An error occurred while installing package 'conda-forge::qt-5.9.7-h506e8af_3' 多个版本的 conda 和 pip 封装 - Multiple versions of conda and pip packages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM