AberSheeran
Aber Sheeran

pip更新后报错

起笔自
所属文集: 程序杂记
共计 1415 个字符
落笔于

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

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看了一眼

#!/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发现了报错的解决方案

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里的引入路径改一下即可恢复正常了。

如果你觉得本文值得,不妨赏杯茶
微软 C++ Build Tools
HTML字体解码错误