金乌智能

让每个人都轻松拥抱爬虫技术,拥有大数据技术!

解决 module 'pip' has no attribute 'main'

python3解决pip升级到10.0.1之后会遇到的错误:module 'pip' has no attribute 'main'


错误:module 'pip' has no attribute 'main'

pip升级到10.0.1之后可能会出现这个错误。

环境:
pip version : 10.0.1
python version : 3.6.4
mac os : 10.12.6 

错误出现路径还原:

升级pip
$python -m pip install pip --upgrade
Requirement already up-to-date: pip in /usr/local/lib/python3.6/site-packages (10.0.1)

升级后使用pip,报错
$pip -V
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2332, in resolve
    return functools.reduce(getattr, self.attrs, module)
AttributeError: module 'pip' has no attribute 'main'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/opt/python/libexec/bin/pip", line 11, in <module>
    load_entry_point('pip==9.0.3', 'console_scripts', 'pip3')()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 480, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2693, in load_entry_point
    return ep.load()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2324, in load
    return self.resolve()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2334, in resolve
    raise ImportError(str(exc))
ImportError: module 'pip' has no attribute 'main'

解决方法:

  • 方法一(未测试)
    网上有类似的错误描述:stackoverflow这里
    里边提到降级处理:
python3 -m pip install --user --upgrade pip==9.0.3

没有亲测这种方式,应该是可以的。

  • 方法二(已测试,有效)
    更新pip
$curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$python get-pip.py

其中,如果是python3对应的pip3,则需要使用python3,如下:

$python3 get-pip.py

更新完毕,便可使用pip或者pip3

$pip -V
pip 10.0.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
$pip3 -V
pip 10.0.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
  • 方法三(未测试)
    网上有说重新安装python3即可解决,这里,未测试。
     brew reinstall python3

方法四 修改pycharm代码:
Mac平台下找到packaging_tool.py(如果其他平台可以按报错查这个文件目录及文件。)

URL:/Applications/PyCharm.app/Contents/helpers/packaging_tool.py

 

好久没打开PyCharm创建项目了,今天打开突然报了一个“AttributeError: module 'pip' has no attribute 'main'”查了资料才知道怎么解决。

vim /Applications/PyCharm.app/Contents/helpers/packaging_tool.py

修改do_install和do_uninstall

 
 
原来:
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)

博文最后更新时间:


评论

  • 暂无评论

发表评论

博客统计

访问量:14972

博文总数:72 评论总数:0

原创72 翻译0 转载0