Python PIP 模块没有属性“main”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43398961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:00:31  来源:igfitidea点击:

PIP module has no attribute "main"

python

提问by DCUFan7

EDIT: The computer in question was a client machine with restrictions on what software could be installed. I'm unsure if that may have been a cause of the issue or if the client's IT department gave the machine a corrupted version of pip. The recommended answers below probably would have worked but were blocked by the company's IT department and required admin login to do. I have since left that project and hope to avoid similar situations.

编辑:有问题的计算机是一台客户端机器,对可以安装的软件有限制。我不确定这是否可能是问题的原因,或者客户的 IT 部门是否给机器提供了损坏的 pip 版本。下面推荐的答案可能会奏效,但被公司的 IT 部门阻止,需要管理员登录才能执行。我已经离开了那个项目,希望避免类似的情况。

I'm attempting to install a WHL file

我正在尝试安装 WHL 文件

While attempting to run:

尝试运行时:

import pip
my_path = <a path to the WHL file>
pip.main(['install', my_path])

I received an Attribute error:

我收到一个属性错误:

'module' object has no attribute 'main'

I ran help(pip) and

我跑了 help(pip) 和

__main__ 

was listed as a package content.

被列为包裹内容。

I'm running Python 3.4 in the console.

我在控制台中运行 Python 3.4。

回答by Alexey ALERT Rubasheff

they made a refactoring. you can support both 9 and 10 pip by using:

他们进行了重构。您可以使用以下方法同时支持 9 和 10 pip:

try:
    from pip import main as pipmain
except:
    from pip._internal.main import main as pipmain

and then use pipmain as you used pip.main. for example

然后像使用 pip.main 一样使用 pipmain。例如

pipmain(['install', "--upgrade", "pip"])
pipmain(['install', "-q", "package"])

回答by coding_idiot

easy_install --upgrade pipworked for me.

easy_install --upgrade pip为我工作。

回答by Anders Larsen

My issue was related to my IDE (PyCharm). older versions of PyCharm does not support pip v10. Upgrading PyCharm solved it for me.

我的问题与我的 IDE (PyCharm) 有关。旧版本的 PyCharm 不支持 pip v10。升级 PyCharm 为我解决了这个问题。

回答by Floyd

For more recent versions of pip (pip>=10.0.0), the functionality described in the other answers will no longer work. I recommend running the pipwith subprocessas follows:

对于较新版本的 pip (pip>=10.0.0),其他答案中描述的功能将不再起作用。我建议按如下方式运行pipwith子进程

import subprocess
import sys

my_path = <a path to the WHL file>
command_list = [sys.executable, "-m", "pip", "install", my_path]
with subprocess.Popen(command_list, stdout=subprocess.PIPE) as proc:
    print(proc.stdout.read())

This solution uses the current python executable to run the pip command as a commandline command. It was inspired by the solution mentioned here.

此解决方案使用当前的 python 可执行文件将 pip 命令作为命令行命令运行。它的灵感来自这里提到的解决方案。

回答by bryant1410

From pip 20.0.0, it's:

从 pip 20.0.0 开始,它是:

from pip._internal.cli.main import main as pipmain