python-pip : ImportError: cannot import name main

9,731

Solution 1

This most likely happened because you were using two different package managers (dpkg or its frontend apt-get, and pip itself) on the same prefix (/usr). I assume that either

  • a recent apt-get upgrade updating some dependencies, but some other files that were installed by pip didn't get updated because dpkg and apt-get simply didn't know about them OR
  • you removed a package with apt-get remove, and as apt-get wasn't aware of anything using this package (as the other package was installed by pip and thus not in the dpkg database), it didn't warn you about breaking other packages.

Without knowing which packages were installed with which package manager, I cannot give you any really specific advice how to repair your system, only general advice:

Don't use multiple different package managers. Only ever use one package manager for a given (sub-)tree of your filesystem, or you will get inconsistencies and/or conflicts. Debian's package manager for the /usr tree is called dpkg, and without calling it noone should ever touch /usr.

It certainly doesn't help that most python-related people you'll encounter try to push pip, even going as far as telling you to sudo pip install; don't. There are distro packages containing python stuff for a reason. Also see this related question on AskUbuntu and this answer on StackOverflow.


You can try to repair the mess by installing a fresh pip with

apt-get purge python-pip python3-pip
apt-get install python-pip python3-pip

however there may be files left behind that might interfere, which dpkg doesn't know about as they were installed by pip. The sure-fire way would be checking all files under /usr/lib/python* whether they belong to a debian package, and if not, delete them without exception.

Again, only use dpkg and its frontend tools like apt-get, aptitude, synaptic, ... to modify /usr. If you need to install anything system-wide that's not available as .deb package, either create a package and install it with dpkg, or install it under /usr/local.

Solution 2

Set python2.7 as default:

sudo update-alternatives --config python

select python2.7.

Remove pip:

sudo python -m pip uninstall pip
sudo apt purge --autoremove python-pip

Reinstall it:

sudo apt install python-pip

Check the pip --version:

pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)

Do not upgrade pip to the latest version.

Share:
9,731

Related videos on Youtube

Siddharth Mishra
Author by

Siddharth Mishra

Student, Physics Enthusiast, Programming Enthusiast...

Updated on September 18, 2022

Comments

  • Siddharth Mishra
    Siddharth Mishra over 1 year

    I am using Debian 9.5 Stretch release. I am having import error while using pip. When I try using pip it says:

    enter image description here

    However I can use python: enter image description here

    I have two versions of python installed: 2.7 and 3.5

    Output of which pip : /usr/bin/pip

    When I tried reinstalling it using apt-get pip worked completely fine for the first time but the next time it shows the same error

    Contents of /usr/bin/

    enter image description here enter image description here

    Don't know whats the problem.

    • Nasir Riley
      Nasir Riley over 5 years
      Add the output of which pip to your question. Also, did you install 3.5 via apt-get or was it compiled?
    • Siddharth Mishra
      Siddharth Mishra over 5 years
      i installed it using apt-get
    • Siddharth Mishra
      Siddharth Mishra over 5 years
      output of which pip : /usr/bin/pip
    • Nasir Riley
      Nasir Riley over 5 years
      In /usr/bin, are is there more than one python and pip such as python and python3 and pip and pip3?
    • Nasir Riley
      Nasir Riley over 5 years
      Try pip install --upgrade pip and pip3 install --upgrade pip.
    • Siddharth Mishra
      Siddharth Mishra over 5 years
      same error while using pip install --upgrade pip or pip3 install --upgrade pip
    • Siddharth Mishra
      Siddharth Mishra over 5 years
    • schaiba
      schaiba over 4 years
      Please don't use screenshots of text.
  • Siddharth Mishra
    Siddharth Mishra over 5 years
    getting errors purging python-pip. Cannot copy the whole log, it's too long
  • Wüstengecko
    Wüstengecko over 5 years
    What's the first actual error message that it displays (with a few lines of pre-context)? Also, try doing apt-get purge python-pip and apt-get purge python3-pip separately.
  • Siddharth Mishra
    Siddharth Mishra over 5 years
    can't use any of those commands. each of them shows some type of error
  • Siddharth Mishra
    Siddharth Mishra over 5 years
    dpkg shows some error about unable to configure pip :Do you want to continue? [Y/n] y (Reading database ... 334637 files and directories currently installed.) Removing python-pip (9.0.1-2) ... File "/usr/bin/pyclean", line 63 except (IOError, OSError), e: ^ SyntaxError: invalid syntax dpkg: error processing package python-pip (--remove): subprocess installed pre-removal script returned error exit status 1
  • Siddharth Mishra
    Siddharth Mishra over 5 years
    removing also shows error : dpkg: error while cleaning up: subprocess installed post-installation script returned error exit status 1 Errors were encountered while processing: python-pip E: Sub-process /usr/bin/dpkg returned an error code (1)
  • Siddharth Mishra
    Siddharth Mishra over 5 years
    SyntaxError: invalid syntax dpkg: error processing package python-pip (--remove): subprocess installed pre-removal script returned error exit status 1 Traceback (most recent call last): File "/usr/bin/pycompile", line 35, in <module> from debpython.version import SUPPORTED, debsorted, vrepr, \ File "/usr/share/python/debpython/version.py", line 24, in <module> from ConfigParser import SafeConfigParser
  • Siddharth Mishra
    Siddharth Mishra over 5 years
    ImportError: No module named 'ConfigParser' dpkg: error while cleaning up: subprocess installed post-installation script returned error exit status 1 Errors were encountered while processing: python-pip E: Sub-process /usr/bin/dpkg returned an error code (1)
  • Siddharth Mishra
    Siddharth Mishra over 5 years
    this was for python-pip
  • Siddharth Mishra
    Siddharth Mishra over 5 years
    Traceback (most recent call last): File "/usr/bin/pycompile", line 35, in <module> from debpython.version import SUPPORTED, debsorted, vrepr, \ File "/usr/share/python/debpython/version.py", line 24, in <module> from ConfigParser import SafeConfigParser ImportError: No module named 'ConfigParser' dpkg: error processing package python-pip (--configure): subprocess installed post-installation script returned error exit status 1 Errors were encountered while processing: python-pip E: Sub-process /usr/bin/dpkg returned an error code (1)
  • Siddharth Mishra
    Siddharth Mishra over 5 years
    for python3-pip
  • GAD3R
    GAD3R over 5 years
    @SiddharthMishra You get the above error because you have set Python3.5 as default , see my update.
  • Siddharth Mishra
    Siddharth Mishra over 5 years
    Each command worked except pip --version, it shows the same import error as mentioned in my question
  • Alex Jansen
    Alex Jansen almost 5 years
    @GAD3R there's a typo here. It should be "sudo apt purge --autoremove python-pip"
  • GAD3R
    GAD3R almost 5 years
    @AlexJohnson Thank you.