简体   繁体   中英

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. I discovered there are many python packages which I installed using pip are actually available on conda-forge.

I would like to replace the pip packages with conda-forge packages. I have been doing this manually but it is too tedious because they are many pip packages. Is there an easy way to replace the pip packages with conda-forge packages automatically with a single command line? 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:

An issue is that conda's repository may not be as comprehensive, and does not cover all versions. 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.

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. 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.

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. Conda environment is explained here 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. I experimented on bash terminal, but I guess similar can be done on the Anaconda prompt or command prompt.

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.

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 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 .

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.
  • Line 4: install the same library from conda-forge.
  • Line 5: if installed from conda-forge, uninstall the 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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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