Error after upgrading pip: cannot import name '

2019-01-02 21:51发布

Whenever I am trying to install any package using pip, I am getting this import error:

guru@guru-notebook:~$ pip3 install numpy
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'


guru@guru-notebook:~$ cat `which pip3`
#!/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())

It was working fine earlier, I am not sure why it is throwing this error. I have searched about this error, but can't find anything to fix it.

Please let me know if you need any further detail, I will update my question.

标签: python pip
19条回答
Explosion°爆炸
2楼-- · 2019-01-02 22:23

Use python -m pip install instead of pip install

The pip (resp. pip3) executable is provided by your distro (python-pip package on Ubuntu 16.04) and located at /usr/bin/pip.

Therefore, it is not kept up-to date with the pip package itself as you upgrade pip, and may break.

If you just use python -m pip directly, e.g. as in:

python -m pip install --user somepackage
python3 -m pip install --user somepackage

it goes through your Python path and finds the latest version of pip, and executes that file.

It relies on the fact that that file is executable through import, but that is a very standard type of interface, and therefore less likely to break than the hackier Debian script.

Then I recommend adding the following aliases to your .bashrc:

pip() ( python -m pip "$@" )
pip3() ( python3 -m pip "$@" )

Tested in Ubuntu 16.04 after an update from pip3 9.0.1 to 18.0.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-02 22:23

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!

查看更多
乱世女痞
4楼-- · 2019-01-02 22:24

I use sudo apt remove python3-pip then pip works.

 ~ sudo pip install pip --upgrade
[sudo] password for sen: 
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'
➜  ~ sudo apt remove python3-pip   
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libexpat1-dev libpython3-dev libpython3.5-dev python-pip-whl python3-dev python3-wheel
  python3.5-dev
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  python3-pip
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 569 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 215769 files and directories currently installed.)
Removing python3-pip (8.1.1-2ubuntu0.4) ...
Processing triggers for man-db (2.7.5-1) ...
➜  ~ pip

Usage:   
  pip <command> [options]
查看更多
Fickle 薄情
5楼-- · 2019-01-02 22:27

I met the same problem on my Ubuntu 16.04 system. I managed to fix it by re-installing pip with the following command:

curl https://bootstrap.pypa.io/get-pip.py | sudo python3

查看更多
该账号已被封号
6楼-- · 2019-01-02 22:27

What worked for me to fix the error with using pip3 was:

sudo cp -v /usr/local/bin/pip3 /usr/bin/pip3

Everything works:

 demon@UbuntuHP:~$ pip -V
 pip 10.0.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

 demon@UbuntuHP:~$ pip2 -V
 pip 10.0.1 from /home/demon/.local/lib/python2.7/site-packages/pip (python 2.7)

 demon@UbuntuHP:~$ pip3 -V
 pip 10.0.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

Maybe the new 10.0.1 version of pip doesn't update the binary in /usr/bin ?

查看更多
The star\"
7楼-- · 2019-01-02 22:28

Trick and works too

sudo -H pip install lxml

查看更多
登录 后发表回答