AttributeError: module 'pip' has no attribute 'main'
通过pycharm创建Django项目是出现如下错误
AttributeError: module 'pip' has no attribute 'main'
环境信息:pycharm2017.3.1(Professional Edition)、python3.6.5 (64位)、pip10.0.1、Django2.0.5
错误原因:由于pip 10版本中没有main(),如果在不降级的情况下,修改这个文件
找到pycharm安装目录下的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 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)
另外一种方式,降级pip的版本
python -m pip install --upgrade pip==9.0.3
---------------------
找到安装目录下 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 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)