python-pip : ImportError: cannot import name main
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 bypip
didn't get updated becausedpkg
andapt-get
simply didn't know about them OR - you removed a package with
apt-get remove
, and asapt-get
wasn't aware of anything using this package (as the other package was installed bypip
and thus not in thedpkg
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.
Related videos on Youtube
Siddharth Mishra
Student, Physics Enthusiast, Programming Enthusiast...
Updated on September 18, 2022Comments
-
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:
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/
Don't know whats the problem.
-
Nasir Riley over 5 yearsAdd the output of
which pip
to your question. Also, did you install 3.5 viaapt-get
or was it compiled? -
Siddharth Mishra over 5 yearsi installed it using apt-get
-
Siddharth Mishra over 5 yearsoutput of which pip : /usr/bin/pip
-
Nasir Riley over 5 yearsIn
/usr/bin
, are is there more than onepython
andpip
such aspython
andpython3
andpip
andpip3
? -
Nasir Riley over 5 yearsTry
pip install --upgrade pip
andpip3 install --upgrade pip
. -
Siddharth Mishra over 5 yearssame error while using
pip install --upgrade pip
orpip3 install --upgrade pip
-
Siddharth Mishra over 5 yearsLet us continue this discussion in chat.
-
schaiba over 4 yearsPlease don't use screenshots of text.
-
-
Siddharth Mishra over 5 yearsgetting errors purging python-pip. Cannot copy the whole log, it's too long
-
Wüstengecko over 5 yearsWhat's the first actual error message that it displays (with a few lines of pre-context)? Also, try doing
apt-get purge python-pip
andapt-get purge python3-pip
separately. -
Siddharth Mishra over 5 yearscan't use any of those commands. each of them shows some type of error
-
Siddharth Mishra over 5 yearsdpkg 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 over 5 yearsremoving 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 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 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 over 5 yearsthis was for python-pip
-
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 over 5 yearsfor python3-pip
-
GAD3R over 5 years@SiddharthMishra You get the above error because you have set Python3.5 as default , see my update.
-
Siddharth Mishra over 5 yearsEach command worked except pip --version, it shows the same import error as mentioned in my question
-
Alex Jansen almost 5 years@GAD3R there's a typo here. It should be "sudo apt purge --autoremove python-pip"
-
GAD3R almost 5 years@AlexJohnson Thank you.