Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pip ImportError: cannot import name 'main' after update #5447

Closed
Mamol27 opened this issue May 28, 2018 · 18 comments
Closed

pip ImportError: cannot import name 'main' after update #5447

Mamol27 opened this issue May 28, 2018 · 18 comments
Labels
resolution: duplicate Duplicate of an existing issue/PR

Comments

@Mamol27
Copy link

Mamol27 commented May 28, 2018

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


Environment

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'
@pradyunsg pradyunsg added the S: needs triage Issues/PRs that need to be triaged label May 31, 2018
@rcor
Copy link

rcor commented Jun 11, 2018

Same issue

@akkshita
Copy link

same issue

@pradyunsg
Copy link
Member

This is the same as #5221.

@pradyunsg pradyunsg added resolution: duplicate Duplicate of an existing issue/PR and removed S: needs triage Issues/PRs that need to be triaged labels Jun 19, 2018
@sydanny
Copy link

sydanny commented Jun 19, 2018

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

@benoit-pierre
Copy link
Member

@sydanny: #5221 (comment)

@pfmoore
Copy link
Member

pfmoore commented Jun 19, 2018

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.

@prayagupa
Copy link

#5221 (comment) is what worked for me

@thangduong
Copy link

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.

@pradyunsg
Copy link
Member

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

@gwuah
Copy link

gwuah commented Jul 25, 2018

hash -d pip
worked for me

@thangduong
Copy link

i uninstalled and reinstalled pip. seems to work.

@vitordouzi
Copy link

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

@linpanusst
Copy link

linpanusst commented Aug 7, 2018

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!

@qubadoff
Copy link

Use pip3 👍

@code3hr
Copy link

code3hr commented Aug 15, 2018

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

@andrewmiller1
Copy link

@linpanusst is correct

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

@SergKolo
Copy link

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'

@pradyunsg
Copy link
Member

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

@pypa pypa locked as off-topic and limited conversation to collaborators Sep 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
resolution: duplicate Duplicate of an existing issue/PR
Projects
None yet
Development

No branches or pull requests