pip.main安装失败,原因是“模块"对象没有属性“主" [英] pip.main install fails with 'module' object has no attribute 'main'

查看:378
本文介绍了pip.main安装失败,原因是“模块"对象没有属性“主"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从python脚本中安装一些python软件包,为此我正在使用pip.main(install).下面是代码段

I am trying to install few of the python packages from within a python script and I am using pip.main(install) for that. Below is code snippet

try:
    import requests
except:
    import pip
    pip.main(['install', '-q', 'requests==2.0.1','PyYAML==3.11'])
    import requests

我尝试使用从pip._internal导入main并使用pipmain代替pip.main(),但这没有帮助.

I have tried using importing main from pip._internal and using pipmain instead of pip.main() but it did not help.

我在pip version 9.0.1python 2.7

推荐答案

pip开发人员不建议从程序内部调用pip. 并且pip.main()方法已从pip v10中删除. 作为一种替代方法,建议在子进程中执行pip.

pip Developers do not recommend calling pip from within the program. And the pip.main() method has been removed from pip v10. As an alternative method it is recommended to execute pip in subprocess.

https://pip .pypa.io/zh-CN/stable/user_guide/?highlight = _internal#using-pip-from-your-program

try:
    import requests
except:
    import sys
    import subprocess
    subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'requests==2.0.1', 'PyYAML==3.11'])
    import requests

这篇关于pip.main安装失败,原因是“模块"对象没有属性“主"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆