Search code examples
python-3.xpipdebianpymc3

Pip error (cannot import name 'main' from 'pip') after upgrading to python 3.8 (Debian 9)


I needed to upgrade Python to install a package (PyMC3) requiring a version>3.5.4. I installed python 3.8.3 without any problem (installing prerequisite libraries, getting file from official repo with wget,using make altinstall, etc), but when I checked python version:

eric@debian:/$ python3 --version
Python 3.6.9 :: Anaconda, Inc.
eric@debian:~$ python3.8 --version
Python 3.8.3

(honestly, i don't remember having installed anaconda in this computer!; according to the pymc3 install error my version was 3.5.4). I tried to fix it up with update-alternatives:

eric@debian:~$ update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                              Priority   Status
------------------------------------------------------------
* 0            /usr/local/bin/python3.8           10        auto mode
  1            /home/eric/anaconda2/bin/python3   2         manual mode
  2            /usr/local/bin/python3.8           10        manual mode

But python3 --version continue throwing Python 3.6.9 :: Anaconda, Inc. When I tried to use pip:

eric@debian:~$ sudo pip3 pymc3
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main' from 'pip' (/usr/local/lib/python3.8/site-packages/pip/__init__.py)

After reading related questions, I tried reinstalling python3-pip but it is already the newest version. I tried to uninstall it with sudo python3 -m pip uninstall pip but it gave me a bunch of red error lines, sudo -H pip3 install --upgrade pip didn't work either (same ImportError for name 'main'). By the way:

eric@debian:~$ which pip3 pip
/usr/bin/pip3
/home/eric/anaconda2/bin/pip
eric@debian:~$ pip --version
pip 19.3.1 from /home/eric/anaconda2/lib/python3.6/site-packages/pip (python 3.6)
eric@debian:~$ pip3 --version
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main' from 'pip' (/usr/local/lib/python3.8/site-packages/pip/__init__.py)

Thanks in advance!

EDIT: After many tries with posts related to this ImportError and PATH environment, I removed Anaconda2 (which was broken) and installed Anaconda3. It works fine, but when I tried pip3 --version:

bash: /usr/bin/pip3: /usr/bin/python3: bad interpreter: No such file or directory

(but it does exist). When I tried to remove pip3 with, for example, sudo apt remove python3-pip:

/bin/sh: 1: /usr/bin/apt-listchanges: not found
E: Sub-process /usr/bin/apt-listchanges --apt || test $? -lt 10 returned an error code (1)
E: Failure running script /usr/bin/apt-listchanges --apt || test $? -lt 10

So I have a problem with apt and, despite many hours reading and trying, i couldn't find a way to remove, upgrade,nor even reinstall apt. Anyway, I guess that this question is no longer suitable for stackoverflow. I will try in superuser or something similar.


Solution

  • I ran into a similar issue, when I was trying to copy a self compiled python build in a docker container to prod machine. I am going to make a guess for the first issue and provide solution for the second:

    Issue 1:
    ImportError: cannot import name 'main' from 'pip' (/usr/local/lib/python3.8/site-packages/pip/__init__.py)

    Here anaconda installed python3 is taking preference over OS level Python3. Since your python3 was not in /usr/bin but pip3 was, I am assuming at sometime, python3 was removed from /usr/bin. Deducted from update here

    Issue 2:
    bash: /usr/bin/pip3: /usr/bin/python3: bad interpreter: No such file or directory

    Basically what is happening here is shebang is pip3 script is pointing to /usr/bin/python3 but on your machine python was NOT available there, so it is failing. Since you made python with altinstall, even pip was renamed to pip3.8 and using that would have worked fine. Alternately, updating the pip3 script with correct python location might have also helped, but on my compiled python the import is as follows: from pip._internal.cli.main import main