Pycharm 报AttributeError: module 'pip' has no attribute 'main'

      最近升级了pip,再建Django项目得时候,Pycharm报AttributeError: module 'pip' has no attribute 'main'的错误,如图:

Pycharm 报AttributeError: module 'pip' has no attribute 'main'_第1张图片

解决办法:根据提示找到pycharm安装目录下的\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)

并替换成以下代码保存:

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 pip.main(['install'] + pkgs)
    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 pip.main(['uninstall', '-y'] + pkgs)
    return main(['uninstall', '-y'] + pkgs)

你可能感兴趣的:(工具使用,Python)