更新pip之後,Pycharm安裝package出現以下錯誤:
Error occurred:
AttributeError:module 'pip' has no attribute 'main'
Proposed solution:
Try to run this command from the system terminal. Make sure that you use the correct version of 'pip' installed for your Python interpreter located at 'E:\python\Python_Demo\venv\Scripts\python.exe'.
command output:
Traceback (most recent call last):
File "D:\Program Files (x86)\PyCharm 2017.3\helpers\packaging_tool.py", line 192, in main
retcode = do_install(pkgs)
File "D:\Program Files (x86)\PyCharm 2017.3\helpers\packaging_tool.py", line 109, in do_install
return pip.main(['install'] + pkgs)
AttributeError: module 'pip' has no attribute 'main'
解決方法如下:
找到安裝目錄下 helpers/packaging_tool.py文件,找到如下代碼:
def do_install(pkgs):
try:
import pip
except ImportError:
error_no_pip()
return pip.main(['install'] + pkgs)
def do_uninstall(pkgs):
try:
import pip
except ImportError:
error_no_pip()
return pip.main(['uninstall', '-y'] + pkgs)
修改爲如下,保存。重啓pycharm,即可完美解決。
def do_install(pkgs):
try:
# import pip
try:
from pip._internal import main
except Exception:
from pip import main
except ImportError:
error_no_pip()
return main(['install'] + pkgs)
def do_uninstall(pkgs):
try:
# import pip
try:
from pip._internal import main
except Exception:
from pip import main
except ImportError:
error_no_pip()
return main(['uninstall', '-y'] + pkgs)