Python AttributeError: 模块 Pip 没有属性“main”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49839610/
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 19:16:19  来源:igfitidea点击:

AttributeError: Module Pip has no attribute 'main'

pythonpip

提问by Kishan Patel

I am trying to build the python api for an open source project called Zulip and I keep running into the same issue as indicated by the screenshot below.

我正在尝试为名为 Zulip 的开源项目构建 python api,但我一直遇到如下屏幕截图所示的相同问题。

I am running python3 and my pip version is 10.0.0. The file in question is setup.pyand the code that is messing up is when the pip.main()attribute is accessed to install a package.

我正在运行 python3,我的 pip 版本是 10.0.0。有问题的文件是setup.py,当pip.main()访问该属性以安装包时,弄乱的代码是。

Now, I know this build should succeed because its an open source project, but I have been trying for hours to fix the dependency issue regarding pip.main().

现在,我知道这个构建应该成功,因为它是一个开源项目,但我一直在尝试解决关于pip.main().

Any help would be greatly appreciated.

任何帮助将不胜感激。

enter image description here

在此处输入图片说明

回答by ericson.cepeda

python3 -m pip install --user --upgrade pip==9.0.3

pip issue: rollback

pip 问题:回滚

回答by mdoc-2011

It appears that pip did a refactor and moved main to internal. There is a comprehensive discussion about it here: https://github.com/pypa/pip/issues/5240

看来 pip 进行了重构并将 main 移到了内部。这里有一个关于它的全面讨论:https: //github.com/pypa/pip/issues/5240

A workaround for me was to change

我的解决方法是改变

import pip
pip.main(...)

to

from pip._internal import main
main(...)

I recommend reading through the discussion, I'm not sure this is the best approach, but it worked for my purposes.

我建议通读讨论,我不确定这是最好的方法,但它对我的目的有效。

回答by mdoc-2011

First run

第一次运行

import pip
pip.__version__

If the result is '10.0.0', then it means that you installed pip successfully
since pip 10.0.0 doesn't support pip.main() any more, you may find this helpful
https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program
Use something like import subprocess subprocess.check_call(["python", '-m', 'pip', 'install', 'pkg']) # install pkg subprocess.check_call(["python", '-m', 'pip', 'install',"--upgrade", 'pkg']) # upgrade pkg

如果结果是'10.0.0',那么这意味着你安装了 pip 成功,
因为 pip 10.0.0 不再支持 pip.main(),你可能会发现这很有帮助
https://pip.pypa.io/ en/latest/user_guide/#using-pip-from-your-program
使用类似 import subprocess subprocess.check_call(["python", '-m', 'pip', 'install', 'pkg']) # install pkg subprocess.check_call(["python", '-m', 'pip', 'install',"--upgrade", 'pkg']) # upgrade pkg



Edit: pip 10.0.1 still doesn't support main
You can choose to DOWNGRADE your pip version via following command:
python -m pip install --upgrade pip==9.0.3

编辑:pip 10.0.1 仍然不支持 main
您可以选择通过以下命令降级您的 pip 版本:
python -m pip install --upgrade pip==9.0.3

回答by Evan

This helps me, https://pip.pypa.io/en/stable/installing/

这对我有帮助,https://pip.pypa.io/en/stable/installing/

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

If you are using python3 and not set it default. do this,

如果您使用的是 python3 并且未将其设置为默认值。做这个,

python3 get-pip.py

It works for me.

这个对我有用。

回答by Shuwn Yuan Tee

To verify whether is your pipinstallation problem, try using easy_installto install an earlier version of pip:

要验证是否是您的pip安装问题,请尝试使用easy_install安装早期版本的 pip:

easy_install pip==9.0.1

If this succeed, pipshould be working now. Then you can go ahead to install any other version of pipyou want with:

如果这成功了,pip现在应该可以工作了。然后你可以继续安装pip你想要的任何其他版本:

pip install pip==10....

Or you can just stay with version 9.0.1, as your project requires version >= 9.0.

或者您可以继续使用version 9.0.1,因为您的项目需要版本 >= 9.0。

Try building your project again.

再次尝试构建您的项目。

回答by Adewole Adesola

My solution is to check the version number of pip and use the import the correct main function correctly

我的解决办法是检查pip的版本号,正确使用import正确的main函数

    import pip

    if int(pip.__version__.split('.')[0])>9:
        from pip._internal import main
    else:
        from pip import main
    def install(package):
        main(['install', package])

回答by AeFinches

If python -m pip install --upgrade pip==9.0.3doesn't work, and you're using Windows,

如果python -m pip install --upgrade pip==9.0.3不起作用,并且您使用的是 Windows,

  1. Navigate to this directory and move the pip folders elsewhere.
  1. 导航到此目录并将 pip 文件夹移动到其他位置。

enter image description here

在此处输入图片说明

  1. Close your IDE if you have it open.

  2. Press 'Repair'on Python 3.

  1. 如果您打开了 IDE,请关闭它。

  2. 在 Python 3 上按“修复”

enter image description here

在此处输入图片说明

  1. Your IDE should cease to detect pip packages and prompt you to install them. Install and keep the last stable pip version by blocking automatic updates. enter image description here
  1. 您的 IDE 应该停止检测 pip 包并提示您安装它们。通过阻止自动更新来安装并保持最后一个稳定的 pip 版本。 在此处输入图片说明

回答by Matthew

Pip 10.0.* doesn't support main.

Pip 10.0.* 不支持 main。

You have to downgrade to pip 9.0.3.

您必须降级到 pip 9.0.3。

回答by Aspak Rogatiya

Try this command.

试试这个命令。

python -m pip install --user pip==9.0.1

回答by Phylliida

For me this issue occured when I was running python while within my site-packages folder. If I ran it anywhere else, it was no longer an issue.

对我来说,这个问题发生在我在站点包文件夹中运行 python 时。如果我在其他任何地方运行它,它就不再是问题。