Jupiter 页面环境下使用pip无法安装(AttributeError: module 'pip' has no attribute 'main')

  • 异常:
  • AttributeError                            Traceback (most recent call last)
    <ipython-input-5-880e5dfa627c> in <module>
          1 def install(package):
          2     pip.main(["install", package])
    ----> 3 install("syslog_parse")
    
    <ipython-input-5-880e5dfa627c> in install(package)
          1 def install(package):
    ----> 2     pip.main(["install", package])
          3 install("syslog_parse")
    
    AttributeError: module 'pip' has no attribute 'main'

     

  • 代码:
  • def install(package):
        pip.main(["install", package])
    
    def uninstall(package):
        pip.main(["uninstall -y", package])

     

  • 解决:这是因为pip不同版本的情况:
  • def install(pkg):
        try:
            from pip._internal import main
        except Exception:
            from pip import main
        return main(["install", pkg])
    
    def uninstall(pkg):
        try:
            from pip._internal import main
        except Exception:
            from pip import main
        return main(["uninstall -y", pkg])

     

posted @ 2019-09-16 18:05  binger0712  阅读(700)  评论(0编辑  收藏  举报