Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"ImportError: No module named conda" #9507

Closed
pitrou opened this issue Dec 13, 2019 · 15 comments
Closed

"ImportError: No module named conda" #9507

pitrou opened this issue Dec 13, 2019 · 15 comments
Labels
locked [bot] locked due to inactivity

Comments

@pitrou
Copy link

pitrou commented Dec 13, 2019

I recently upgrade conda from 4.7.12 to 4.8.0 and now, every time I open a shell, I get this error:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
ImportError: No module named conda

Weirdly, conda still works:

(base) antoine@orphee:~$ conda info

     active environment : base
    active env location : /home/antoine/miniconda3
            shell level : 1
       user config file : /home/antoine/.condarc
 populated config files : 
          conda version : 4.8.0
    conda-build version : not installed
         python version : 3.7.5.final.0
       virtual packages : __glibc=2.29
       base environment : /home/antoine/miniconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /home/antoine/miniconda3/pkgs
                          /home/antoine/.conda/pkgs
       envs directories : /home/antoine/miniconda3/envs
                          /home/antoine/.conda/envs
               platform : linux-64
             user-agent : conda/4.8.0 requests/2.22.0 CPython/3.7.5 Linux/5.0.0-37-generic ubuntu/19.04 glibc/2.29
                UID:GID : 1000:1000
             netrc file : None
           offline mode : False

If I remove the snippet added by conda init at the end of my .bashrc, the error disappears (but of course conda is not in the PATH anymore). If I run conda init again, the error reappears.

@pitrou
Copy link
Author

pitrou commented Dec 13, 2019

This is the snippet originally added by conda init to .bashrc:

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/antoine/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/antoine/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/home/antoine/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/antoine/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

If I comment out some of the lines thusly:

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
#__conda_setup="$('/home/antoine/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
#if [ $? -eq 0 ]; then
#    eval "$__conda_setup"
#else
    if [ -f "/home/antoine/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/home/antoine/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/antoine/miniconda3/bin:$PATH"
    fi
#fi
#unset __conda_setup
# <<< conda initialize <<<

then the error disappears and the conda install still works.

@QuentinSoubeyran
Copy link

QuentinSoubeyran commented Dec 14, 2019

Hi there, I got the same error and investigated a bit. Short version of my findings at the end.

  • This is indeed caused by upgrading conda from 4.7.12 to 4.8, tested after completely removing conda (using anaconda-clean, rm -rf ~/anaconda3 and removing the conda init lines from ~/.bashrc), re-installing conda with the installer and upgrading conda.

    • I first tried conda install pylint, which only upgrades conda. The error occures afterwards
    • I then tried conda upgrade conda. Two other packages are upgraded with conda, but the error still appears
  • The problems comes from the eval "$__conda_setup" line of .bashrc. Using echo, I saw that the __conda_setup returned by the shell.bash hook differ vastly from conda 4.7.12 and conda 4.8. Conda 4.8 sources <conda_install>/etc/bash_completion.d/conda which does not exist under conda 4.7.12. In this file, the problematic lines seems to be:

# line 26
function __comp_conda_ensure_root() {
   # [...]
        CONDA_SOURCE=$(
            cat - <<'            EOF' | sed 's/^ *: //g' | python -
            : from __future__ import print_function
            : import os
            : import conda # <--  THERE
            : print(os.path.dirname(conda.__file__))
            EOF
        )
    # [...]
}

I added a raise statement (print doesn't work) as follows:

# line 26
function __comp_conda_ensure_root() {
   # [...]
        CONDA_SOURCE=$(
            cat - <<'            EOF' | sed 's/^ *: //g' | python -
            : from __future__ import print_function
            : import os
           # ADDED LINES
            : import sys
            : raise ValueError("python: " + sys.version)
           # END ADDED LINES
            : import conda # <--  THERE
            : print(os.path.dirname(conda.__file__))
            EOF
        )
    # [...]
}

While this obviously halts the execution, it showed that the python version was 2.7 - which I'm pretty sure should not be the case, this is a system python version, not the anaconda one.

When looking at the PATH just before the function is called, here in the same file:

# Lline 100
# cache conda subcommand help lookups for the duration of the shell
unset __comp_conda_cache
declare -A __comp_conda_cache
echo "$PATH" # ADDED LINE
__comp_conda_ensure_root

and comparing with the PATH after the shell has started up, during that execution thePATH is missing ~/anaconda3/bin, which is present after the shell has started.

TL;DR: The problem seems to be that the new file ~/anaconda3/etc/bash_completion.d/conda added by conda 3.8.0 is sourced before ~/anaconda3/bin is prefixed to the PATH, so the python used is the system python which naturally lacks the conda module.

Hope this helps resolve the issue !

EDIT:

  • conda installer: Anaconda3-2019.10-Linux-x86_64.sh

@jeanmonet
Copy link

jeanmonet commented Dec 14, 2019

Same here (just updated to 4.8). The bug does not seem to affect any conda commands - all seems to work even though I get this error each time I log into shell (Ubuntu 18.04 LTS):

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
ImportError: No module named conda
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
ImportError: No module named conda
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
ImportError: No module named conda

@piglesias1
Copy link

I had the same issue after the conda update to 4.8.0. I was checking the init script and could reach the same conclusion as @QuentinSoubeyran. As the python version is from the system and not from the conda package, due to the path was not set before, I did a simple workaround:

change the line 36 of <conda_install>/etc/bash_completion.d/conda from:

        CONDA_SOURCE=$(
            cat - <<'            EOF' | sed 's/^ *: //g' | python -  # <<< Original
            : from __future__ import print_function
            : import os
            : import conda
            : print(os.path.dirname(conda.__file__))
            EOF
        )

to:

        CONDA_SOURCE=$(
            cat - <<'            EOF' | sed 's/^ *: //g' | ${CONDA_PYTHON_EXE} -  # <<< Changed
            : from __future__ import print_function
            : import os
            : import conda
            : print(os.path.dirname(conda.__file__))
            EOF
        )

Now seems to be working normally and I don't have the error message when starting the shell.

@MMStojiljkovic
Copy link

I can report the same problem on Ubuntu 18.04. After downgrading back to conda 4.7.12, it works fine.

@forrestwaters
Copy link
Contributor

Looks like this is fixed in master by #9421 but was not included in the 4.8 release

@Brinkhuis
Copy link

Issue seems solved by todayś update conda update --all

@obarak
Copy link

obarak commented Dec 17, 2019

Issue seems solved by todayś update conda update --all

conda update conda in your base environment is enough, don't have to update all other packages :-)
BTW, the new package is still tagged 4.8.0 only with build py37_1 instead of py37_0

@hmaarrfk
Copy link
Contributor

That fix only works for people using conda-forge. but I guess using conda-forge is your choice.

@jjhelmus
Copy link
Contributor

defaults also has a patched build.

@msarahan
Copy link
Contributor

fixed in 4.8.1, which is on conda-canary. Closing.

@Vivek1717
Copy link

Vivek1717 commented Feb 6, 2020

I am still getting the error:
C:\ProgramData\Anaconda3\condabin>conda update conda
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\Scripts\conda-script.py", line 11, in
from conda.cli import main
ModuleNotFoundError: No module named 'conda'

I have installed Anaconda3-2019.10-Windows-x86_64 on windows 10

@QuentinSoubeyran
Copy link

QuentinSoubeyran commented Feb 7, 2020

Hi @Vivek1717 ,
From the looks of it, you are getting a different error. The original error was a curious print when opening a console, with conda still working properly.

You have the conda update conda command (and possibly others) crashing on execution.

I would suggest looking for your specific error online: this might help. If the problem is indeed due to your PATH, here is how to diagnose and solve it on windows. From memory, I also recall something about administrator privileges causing problems if conda was installed with admin rights and is run without.

If you still experience the problem, it is best to create a dedicated issue to keep things organised.

EDIT: added further references to potential solutions

@Armughan-Shahid-AAI
Copy link

I tried the solutions stated but was unable to resolve the issue through them. However this post worked like a charm for me.

@github-actions
Copy link

Hi there, thank you for your contribution to Conda!

This issue has been automatically locked since it has not had recent activity after it was closed.

Please open a new issue if needed.

@github-actions github-actions bot added the locked [bot] locked due to inactivity label Aug 22, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked [bot] locked due to inactivity
Projects
None yet
Development

No branches or pull requests