pip更新后报错

最后更新时间: 2018-10-12 | 作者: AberSheeran | 捐助

使用python -m pip install --upgrade pip更新pip到最新版后,再次使用pip产生了报错

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

很奇怪,于是cat /usr/bin/pip看了一眼

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/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())

进入Python交互式命令行,通过sys.path找到site-packages的路径,进入之后,发现pip从一个文件变成了目录,更准确的说是Python的一个包。查看__init__.py发现只有版本信息。然后cat __main__.py发现了报错的解决方案

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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())

原来main改到了pip._internal里。那么只需要把/usr/bin/pip里的引入路径改一下即可恢复正常了。

标签: Python
收录于#杂记