0

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.

  • thry this `apt install python3-pip` –  May 20 '20 at 06:43
  • Done, but it fails: `"Reading package lists... Done Building dependency tree Reading state information... Done python3-pip is already the newest version (9.0.1-2+deb9u1). 0 upgraded, 0 newly installed, 0 to remove and 101 not upgraded. 1 not fully installed or removed. After this operation, 0 B of additional disk space will be used. Do you want to continue? [Y/n] y /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` – E Speranza May 21 '20 at 14:09

1 Answers1

0

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

ghitesh
  • 160
  • 2
  • 14
  • Thanks! The first issue was solved after reinstalling Anaconda. Regarding the second issue, you're right: pip3 was not pointing to python folder. I managed to solve the pip3 problem: `pip3 --version pip 20.2.2 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)` and it has the same import line that you've mentioned. But pip file is identical and it's located on the same folder: `pip --version pip 20.2.2 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)` Is it ok? Or should it be pointing to my python 2.7 (which is at usr/bin)? – E Speranza Aug 28 '20 at 20:26
  • I have come to realize that python version management is a complete nightname, unless .. Anyways to give you my view, run `which pip` or `which pip3`, ideally it should be in the same location as your `which python` or `which python3` or `which python3.8` and the shebang should point to the same location. Any other value and it is liable to break again once you install python or for some reason $PATH changes. – ghitesh Sep 03 '20 at 06:11
  • I will check the shebangs and I will try to fix it (currently both pip and pip3 are pointing to python 3.8, since both are in the same folder (I have a pip in the python (2,7) folder but it is not in use. Now I have an issue related to apt-get (that arose while trying to fix all this mess). I've posted it as a different question (https://unix.stackexchange.com/questions/597833/problems-with-apt-listchanges-after-python-upgrade-debian). Thanks! – E Speranza Sep 06 '20 at 20:17