简体   繁体   中英

Restore conda/pip freeze consistency

Until now I was managing my Python envs with conda and was using:

pip freeze > requirements.txt

when needed for deployment purposes.

But I ended up doing pip install some_package for some package I could not install with conda (which was apparently a terrible idea) and now pip freeze result has become totally unrelated to whichever conda environment I am activating.

Is there a way I can restore the situation, meaning having pip freeze correctly describe the conda environment that is currently active?

When using both pip and conda I would recommend using conda env export > environment.yml . This will create a yml file with the versions you are using including where to find them and how to install them.

When you need to install the enviroment on a new machine, you can use conda env create -f environment.yml

Note: the yml file also dictates what the environment will be called. This can be changed manually in the file before installing the environment though.

I could solve the problem following: https://github.com/ContinuumIO/anaconda-issues/issues/1429#issuecomment-320117143

The main idea is to install pip in your conda env before doing any pip install and then deactivating conda before using it. In details:

Step 1 : Deactivate conda

conda deactivate

repeatedly until no longer in a conda env (nothing in parentheses to the left of your prompt) or use

source deactivate (deprecated but still works)

Then

Step 2 :

conda activate your_environment
conda install pip
which pip
=> /home/sunless/miniconda3/bin/conda (global version)
pip freeze
=> still your global packages

Step 3 : Deactivate your env again (same as step 1)

Step 4 :

conda activate your_environment
which pip
=> /home/sunless/miniconda3/envs/your_environment/bin/pip (local version)
pip freeze
=> your local packages

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