Search code examples
ubuntucondaminiconda

No module named 'conda' after conda update


I have installed miniconda3 in my ubuntu machine. When I use conda then it gives following error:

 File "/home/himal/miniconda3/bin/conda", line 12, in <module>
    from conda.cli import main
ModuleNotFoundError: No module named 'conda'

But code in jupyter note book from virtual environment (miniconda3/envs/himal) works. I am facing problem install new package inside virtual environment 'himal'. How to change base environment in Ubuntu?

enter image description here

EDIT

The output of

$ echo ~/miniconda3/lib/python*/site-packages/conda-* 

is

/home/himal/miniconda3/lib/python3.7/site-packages/conda-4.6.1-py3.7.egg-info

AND

~/miniconda3/bin/python --version

is

Python 3.8.2

Python version inside virtual envs is 3.7.2. While I use jupyter notebook from Pycharm virtual envs works. But problem while using conda from terminal


Solution

  • The problem here likely arose from a bug in conda. The interpreter was updated to version 3.8, but conda is still installed into the python 3.7 interpreter and thus cannot be found anymore.

    First we need to find out the path to the current conda library. This can be done by following command: echo ~/miniconda3/lib/python*/site-packages/conda-*. We then set up two helper environment variables:

    # path to site package with conda library
    SITE_PACKAGES=/home/himal/miniconda3/lib/python3.7/site-packages
    # path to current miniconda base python interpreter
    PYTHON_BASE=/home/himal/miniconda3/bin/python
    

    We then need to find the last working revision

    PYTHONPATH=$SITE_PACKAGES $PYTHON_BASE -m conda list --revisions
    

    Find the revision id for the last conda transaction. For me this would be transaction 2020-04-03 08:19:57 (rev 107) the id we need is 107.

    Now we can revert to the previous revision (107 - 1 = 106):

    PYTHONPATH=$SITE_PACKAGES $PYTHON_BASE -m conda install --revision 106
    

    After accepting this, conda should be back.

    From here, update conda to prevent this from happening in the future:

    conda install conda python=3.7