更新pip之后,Pycharm安装package出现报错:module 'pip' has no attribute 'main'
找到安装目录下 helpers/packaging_tool.py文件,找到如下代码:
1 def do_install(pkgs):
2 try:
3 import pip
4 except ImportError:
5 error_no_pip()
6 return pip.main(['install'] + pkgs)
7
8
9 def do_uninstall(pkgs):
10 try:
11 import pip
12 except ImportError:
13 error_no_pip()
14 return pip.main(['uninstall', '-y'] + pkgs)
15
修改为如下,保存即可。
1
2 def do_install(pkgs):
3 try:
4 # import pip
5 try: #修改位置1
6 from pip._internal import main
7 except Exception:
8 from pip import main
9 except ImportError:
10 error_no_pip()
11 return main(['install'] + pkgs) #修改位置2
12
13
14 def do_uninstall(pkgs):
15 try:
16 # import pip
17 try: #修改位置3
18 from pip._internal import main
19 except Exception:
20 from pip import main
21 except ImportError:
22 error_no_pip()
23 return main(['uninstall', '-y'] + pkgs) #修改位置4