pip ImportError: cannot import name 'main' after update

Created on 28 May 2018  ·  18Comments  ·  Source: pypa/pip

Maintainer note: Anyone that still gets this issue please see #5599.


Environment

  • pip version: ???
  • Python version: 3.5.2
  • OS: Linux sas-linuxmint 4.4.0-92-generic #115-Ubuntu SMP Thu Aug 10 09:04:33 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
sas@sas-linuxmint /usr/bin $ pip3 install --user --upgrade pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-8.1.1
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.
ImportError: cannot import name 'main'
sas@sas-linuxmint /usr/bin $ pip3 install --upgrade pip
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'
duplicate

Most helpful comment

hash -d pip
worked for me

All 18 comments

Same issue

same issue

This is the same as #5221.

@pradyunsg Why are you marking this a duplicate of a closed issue? It's clearly not closed if it's still an issue...

In this case, the particular issue seems to be:

  1. pip3 install --user --upgrade pip installs pip 10 in the user site, but doesn't uninstall the system site copy of pip.
  2. User runs the system wrapper from /usr/bin/pip3 which is from the OS-supplied pip 8. This wrapper expects to see pip 8, but it doesn't because user site takes priority over system site.

The solution is to use the pip wrapper installed when you installed pip 10 in --user. That will mean changing your PATH to put that first, or using an explicit path when you invoke pip.

Technically the issue here is slightly different from #5221, as in that issue users were overwriting the system pip. But the root cause is the same - using an older pip wrapper (supplied by the OS) against a newer version of pip. That's not a supported scenario, and you can't encounter it with correct use of pip. So there's no pip issue, hence we close the issues when they arise. We're directing users at #5221 because that's where the bulk of the advice on how to tidy up your system after an incorrect upgrade is located.

thang@cq-gpu:/mnt/tmp/tqdscripts/azure/nvtf$ pip3 install --user --upgrade pip
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in
from pip import main
ImportError: cannot import name 'main'

This does nothing for me. Still same problem.

Please take a look at #5599 and issues linked there.

hash -d pip
worked for me

i uninstalled and reinstalled pip. seems to work.

I had the same problem, but uninstall and reinstall with apt and pip didn't work for me.

I saw another solution on stackoverflow that presents a easy way to recover pip3 path:

sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall

Hi, fellows! I have the same problem and solved it. Here is my solution.
First, when I run pip install something, the error came out like this:

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

So, I cd into the file /usr/bin/ and cat pip3 to see the code in it. I see this in it:

`#!/usr/bin/python3
# GENERATED BY DEBIAN
import sys
# Run the main entry point, similarly to how setuptools does it, but because
# we didn't install the actual entry point from setup.py, don't use the
# pkg_resources API.
from pip import main
if __name__ == '__main__':
    sys.exit(main())
`

And then I think that it was not in the installation path. So I cd into the python3-pip, like this:

cd /.local/lib/python3.5/site-packages/pip
ps: you have to cd into the right directions in your computer
Then I cat the file to see the differences(you can use other operations to see the code):

cat __main__.py

And I saw this:

`from __future__ import absolute_import
import os
import sys
# If we are running from a wheel, add the wheel to sys.path
# This allows the usage python pip-*.whl/pip install pip-*.whl
if __package__ == '':
    # __file__ is pip-*.whl/pip/__main__.py
    # first dirname call strips of '/__main__.py', second strips off '/pip'
    # Resulting path is the name of the wheel itself
    # Add that to sys.path so we can import pip
    path = os.path.dirname(os.path.dirname(__file__))
    sys.path.insert(0, path)

from pip._internal import main as _main  # isort:skip # noqa

if __name__ == '__main__':
    sys.exit(_main())
`

So, can you see the difference? I can figure out that I have to make the file the same as the file in /usr/bin/pip3

So, I copped the code in /.local/lib/python3.5/site-packages/pip to replace the code in /usr/bin/pip3
and the problem disappear!

ps: pip3 or pip have no difference in this problem.
I will be happy if my solution solve your problem!

Use pip3 :+1:

you should try using
python3 -m pip install --user "packagename"

@linpanusst is correct

sudo ln -sf $( type -P pip ) /usr/bin/pip

Same issue persists here. Deepin OS, 15.7. The interesting bit is that as regular user it fails but seems to work with sudo just fine:

$ pip3 search audioshare
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'
$ sudo pip3 search librosa
librosa (0.6.2)  - Python module for audio and music processing

The suggested workaround sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall made no difference.

A solution posted on stackoverflow suggests editing /usr/bin/pip3 helps the regular user, but breaks sudo:

$ sudo pip3 search librosa
Traceback (most recent call last):
  File "/usr/bin/pip3", line 13, in <module>
    sys.exit(__main__._main())
AttributeError: module 'pip.__main__' has no attribute '_main'

Please take a look at #5599 and issues linked there.

Was this page helpful?
0 / 5 - 0 ratings