1

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.

  • Add the output of which pip to your question. Also, did you install 3.5 via apt-get or was it compiled? – Nasir Riley Nov 4 '18 at 2:11
  • i installed it using apt-get – Siddharth Mishra Nov 4 '18 at 2:15
  • output of which pip : /usr/bin/pip – Siddharth Mishra Nov 4 '18 at 2:16
  • In /usr/bin, are is there more than one python and pip such as python and python3 and pip and pip3? – Nasir Riley Nov 4 '18 at 2:21
  • same error for pip3 – Siddharth Mishra Nov 4 '18 at 2:21
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.

  • getting errors purging python-pip. Cannot copy the whole log, it's too long – Siddharth Mishra Nov 4 '18 at 6:06
  • 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. – Wüstengecko Nov 4 '18 at 18:35
  • 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 Nov 5 '18 at 4:01
  • 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 Nov 5 '18 at 4:01
  • this was for python-pip – Siddharth Mishra Nov 5 '18 at 4:02
1

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.

  • can't use any of those commands. each of them shows some type of error – Siddharth Mishra Nov 5 '18 at 3:46
  • 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 Nov 5 '18 at 3:49
  • 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 Nov 5 '18 at 3:49
  • @SiddharthMishra You get the above error because you have set Python3.5 as default , see my update. – GAD3R Nov 5 '18 at 9:21
  • 1
    @GAD3R there's a typo here. It should be "sudo apt purge --autoremove python-pip" – Alex Jansen May 30 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.