After Anaconda installation, conda command fails with "ImportError: no module named conda.cli"
Question:
I installed 64 bit Linux version of Anaconda recently (1.8.0-Linux-x86_64). The installation seemed to work fine:
$ python
Python 2.7.5 |Continuum Analytics, Inc.| (default, Nov 4 2013, 15:30:26)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
##No issues here
However if I try any of the conda commands, I get an error:
$ conda info
Traceback (most recent call last):
File "~/anaconda/bin/conda", line 3, in <module>
from conda.cli import main
ImportError: No module named conda.cli
Installation is under my user directory (~/anaconda). I have verified that $PATH contains ~/anaconda/bin. $PYTHONPATH is also set to ~/anaconda/lib.
Any thoughts on what is wrong with the ‘conda’ command? My searches do not appear to show any one else reporting this error.
Answers:
When you were installing it, you missed a section. When conda asked you if it can set your .bashrc file, you probably clicked NO. It’s a simple fix: fire up your interpreter and add this line:
export PATH=/home/add your username here/anaconda/bin:$PATH
Now type python
into the interpreter and you will see Anaconda 1.8.0 or whatever version you have. You will have to do this each time you start a new interpreter.
Enjoy!
If anaconda was installed by the root user, a permissions issue could produce the ImportError: No module named 'conda.cli'
error.
Something like this
sudo chown -R [username] [anaconda_dir]
would help in that case.
Alternatively, the conda install could be cloned to another location, such as the user’s home directory:
conda create -n [my_conda_root] --clone=[current_conda_dir]
But for this to work, you will need at least the read and traverse permissions.
Note
If the accepted answer doesn’t work, then this answer might help.
The Cause
According to the official thread on GitHub, this problem happens when there is a change in the python version. In my case, the original anaconda installation is anaconda 3 with python3.6
. And I installed a new package which updated python topython3.7
along with some other packages. And this corrupts the anaconda base environment.
The solution
The solution that works for me is pretty simple… reinstall anaconda.
I don’t know if this is the optimal solution, but this what works for me.
- First we need to delete the old installation:
rm -rf ~/anaconda3
rm -rf ~/.condarc ~/.conda ~/.continuum
-
Then, reinstall Anaconda.
-
Finally, update all packages (including conda itself) using:
conda update --all
Now, your anaconda is up-to-date.
I ran into this problem while downgrading python in a Miniconda3 env. in my Dockerimage. With the Miniconda3-4.5.11-Linux-x86_64.sh installer the initial base python version is 3.7.0; I wanted to downgrade to 3.6.9 and got the “ImportError: no module named conda.cli” error (all in the same layer to reduce the image’s size).
I fix this by using the Miniconda3-4.5.4-Linux-x86_64.sh installer instead (which will give you python==3.6.5) and called
conda install -y python==3.6.9
I am using Anaconda and I found this Solution from somewhere:
Change the content of: /opt/cloudera/parcels/Anaconda-5.2.0/bin/conda
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import sys
from conda.cli import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script.pyw?|.exe)?$', '', sys.argv[0])
sys.exit(main())
to
#!/opt/cloudera/parcels/Anaconda/bin/python
# -*- coding: utf-8 -*-
import sys
# Before any more imports, leave cwd out of sys.path for internal 'conda shell.*' commands.
# see https://github.com/conda/conda/issues/6549
if len(sys.argv) > 1 and sys.argv[1].startswith('shell.') and sys.path and sys.path[0] == '':
# The standard first entry in sys.path is an empty string,
# and os.path.abspath('') expands to os.getcwd().
del sys.path[0]
if __name__ == '__main__':
from conda.cli import main
sys.exit(main())
I fixed it by reinstalling anaconda but keeping the envs
folder.
So, I moved the old anaconda3
folder to anaconda3.old
, I reinstalled anaconda3 at the same location and copied the content of the anaconda3.old/envs
folder to anaconda3/envs
. Then it worked.
I had a similar problem:
Traceback (most recent call last):
File "/home/ubuntu/anaconda3/bin/conda", line 7, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
It happened on Ubuntu (both versions 22.04 and 20), while trying to upgrade Python from the default version that comes with Anaconda 3-5.3.1, e.g. using:
conda update python
Turns out upgrading Python is a no-no on conda, and I finally had to uninstall anaconda:
rm -rf ~/anaconda3 ~/.condarc ~/.conda ~/.continuum
And then install a newer version (e.g. Anaconda3-2022.05-Linux-x86_64.sh, see here on how).
An "interesting" side of this problem is that sometimes Python upgrade happens implicitly as a dependency. For example do not:
conda install pandas==1.4.2
pip install pandas==1.4.2
conda install gunicorn
Instead – use a higher version of Anaconda.
To be honest, I have tried almost every way but none works. The only solution is to force reinstall the conda into your machine so that it would automatically set the path/source into your environment. In, any other case, it ends up with broken conda installation.
So,
- uninstall anaconda –
rm -rf ~/anaconda3
- uninstall anaconda continuum –
rm -rf ~/.condarc ~/.conda ~/.continuum
- Force reinstall anaconda –
bash Anaconda3-2018.12-Linux-x86_64.sh -u
Note: Change .sh file version according to the file you have
For me, I had zsh (zshell) and bash, so the "conda" command was working fine in bash but not in the zsh. so, I added the environment variable in the zsh file.
Below were the steps that I took:
1.
sudo nano ~/.zshrc
add the below line at the end of the file, make sure to change it based on where you install the anaconda
export PATH=~/anaconda3/bin:$PATH
3.
source ~/.zshrc
I installed 64 bit Linux version of Anaconda recently (1.8.0-Linux-x86_64). The installation seemed to work fine:
$ python
Python 2.7.5 |Continuum Analytics, Inc.| (default, Nov 4 2013, 15:30:26)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
##No issues here
However if I try any of the conda commands, I get an error:
$ conda info
Traceback (most recent call last):
File "~/anaconda/bin/conda", line 3, in <module>
from conda.cli import main
ImportError: No module named conda.cli
Installation is under my user directory (~/anaconda). I have verified that $PATH contains ~/anaconda/bin. $PYTHONPATH is also set to ~/anaconda/lib.
Any thoughts on what is wrong with the ‘conda’ command? My searches do not appear to show any one else reporting this error.
When you were installing it, you missed a section. When conda asked you if it can set your .bashrc file, you probably clicked NO. It’s a simple fix: fire up your interpreter and add this line:
export PATH=/home/add your username here/anaconda/bin:$PATH
Now type python
into the interpreter and you will see Anaconda 1.8.0 or whatever version you have. You will have to do this each time you start a new interpreter.
Enjoy!
If anaconda was installed by the root user, a permissions issue could produce the ImportError: No module named 'conda.cli'
error.
Something like this
sudo chown -R [username] [anaconda_dir]
would help in that case.
Alternatively, the conda install could be cloned to another location, such as the user’s home directory:
conda create -n [my_conda_root] --clone=[current_conda_dir]
But for this to work, you will need at least the read and traverse permissions.
Note
If the accepted answer doesn’t work, then this answer might help.
The Cause
According to the official thread on GitHub, this problem happens when there is a change in the python version. In my case, the original anaconda installation is anaconda 3 with python3.6
. And I installed a new package which updated python topython3.7
along with some other packages. And this corrupts the anaconda base environment.
The solution
The solution that works for me is pretty simple… reinstall anaconda.
I don’t know if this is the optimal solution, but this what works for me.
- First we need to delete the old installation:
rm -rf ~/anaconda3 rm -rf ~/.condarc ~/.conda ~/.continuum
-
Then, reinstall Anaconda.
-
Finally, update all packages (including conda itself) using:
conda update --all
Now, your anaconda is up-to-date.
I ran into this problem while downgrading python in a Miniconda3 env. in my Dockerimage. With the Miniconda3-4.5.11-Linux-x86_64.sh installer the initial base python version is 3.7.0; I wanted to downgrade to 3.6.9 and got the “ImportError: no module named conda.cli” error (all in the same layer to reduce the image’s size).
I fix this by using the Miniconda3-4.5.4-Linux-x86_64.sh installer instead (which will give you python==3.6.5) and called
conda install -y python==3.6.9
I am using Anaconda and I found this Solution from somewhere:
Change the content of: /opt/cloudera/parcels/Anaconda-5.2.0/bin/conda
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import sys
from conda.cli import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script.pyw?|.exe)?$', '', sys.argv[0])
sys.exit(main())
to
#!/opt/cloudera/parcels/Anaconda/bin/python
# -*- coding: utf-8 -*-
import sys
# Before any more imports, leave cwd out of sys.path for internal 'conda shell.*' commands.
# see https://github.com/conda/conda/issues/6549
if len(sys.argv) > 1 and sys.argv[1].startswith('shell.') and sys.path and sys.path[0] == '':
# The standard first entry in sys.path is an empty string,
# and os.path.abspath('') expands to os.getcwd().
del sys.path[0]
if __name__ == '__main__':
from conda.cli import main
sys.exit(main())
I fixed it by reinstalling anaconda but keeping the envs
folder.
So, I moved the old anaconda3
folder to anaconda3.old
, I reinstalled anaconda3 at the same location and copied the content of the anaconda3.old/envs
folder to anaconda3/envs
. Then it worked.
I had a similar problem:
Traceback (most recent call last):
File "/home/ubuntu/anaconda3/bin/conda", line 7, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
It happened on Ubuntu (both versions 22.04 and 20), while trying to upgrade Python from the default version that comes with Anaconda 3-5.3.1, e.g. using:
conda update python
Turns out upgrading Python is a no-no on conda, and I finally had to uninstall anaconda:
rm -rf ~/anaconda3 ~/.condarc ~/.conda ~/.continuum
And then install a newer version (e.g. Anaconda3-2022.05-Linux-x86_64.sh, see here on how).
An "interesting" side of this problem is that sometimes Python upgrade happens implicitly as a dependency. For example do not:
conda install pandas==1.4.2
pip install pandas==1.4.2
conda install gunicorn
Instead – use a higher version of Anaconda.
To be honest, I have tried almost every way but none works. The only solution is to force reinstall the conda into your machine so that it would automatically set the path/source into your environment. In, any other case, it ends up with broken conda installation.
So,
- uninstall anaconda –
rm -rf ~/anaconda3
- uninstall anaconda continuum –
rm -rf ~/.condarc ~/.conda ~/.continuum
- Force reinstall anaconda –
bash Anaconda3-2018.12-Linux-x86_64.sh -u
Note: Change .sh file version according to the file you have
For me, I had zsh (zshell) and bash, so the "conda" command was working fine in bash but not in the zsh. so, I added the environment variable in the zsh file.
Below were the steps that I took:
1.
sudo nano ~/.zshrc
add the below line at the end of the file, make sure to change it based on where you install the anaconda
export PATH=~/anaconda3/bin:$PATH
3.
source ~/.zshrc