5

I'm trying to install a python application I wrote on a freshly created Ubuntu 16.4 virtual instance. I need to install a number of python packages that I previously installed on my own PC.

Everything was going along swimmingly, until I saw this message:

You are using pip version 8.1.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Okay, thinks I, this looks harmless enough.

However, after doing this, the next time I run pip, I get:

$ pip freeze
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main

I thought I should revert to the old pip so

$ sudo apt install python-pip

and then

$ sudo apt install python-pip
...
Setting up python-pip (8.1.1-2ubuntu0.4) ...

OK, I've got pip 8.1.1 back. But alas

$ pip freeze
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main

So the question is what got screwed up by the upgrade of pip and how do I fix it?

3
 $python -m pip uninstall pip

Use this and it will give you the previous pip version.

2

Recently ran into the same issue with Ubunutu 16.04.

Notice when you do:

/usr/bin/pip freeze

You will get the Traceback error. When I ran which pip, I got the following:

/usr/local/bin/pip

My shell was using /usr/bin/pip which was not the right one. I renamed /usr/bin/pip and symlinked the new one to where the old one was located:

ln -s /usr/local/bin/pip /usr/bin/pip

Once I ran pip freeze, boom no more errors. I was able to see the packages installed.

0

I don't know how it worked, but I edited my /usr/bin/pip file and changed the offending line 7:

from pip import main

to

from pip._internal import main

and it this solved the issue.

I am using Ubuntu on Windows 10.

As mentioned in another answer, this was the difference between /usr/local/bin/pip and /usr/bin/pip in my machine.

0
sudo python3 -m pip install --force-reinstall pip

This will fix the problem of incorrect pip being selected.

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.