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