Python 导入错误:在 windows7 32 位中运行 pip --version 命令时无法导入名称 main

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

ImportError: cannot import name main when running pip --version command in windows7 32 bit

pythonpipimporterror

提问by Woootiness

I've installed the latest python (2.7.9) bundled with pip and setuptools for windows 32-bit. I've tried reinstalling pip but the problem persists.

我已经安装了最新的 python (2.7.9),与 pip 和 setuptools 捆绑在一起,适用于 Windows 32 位。我试过重新安装 pip 但问题仍然存在。

Here's the error after running pip --versionin Administrator cmd:

这是pip --version在管理员cmd中运行后的错误:

Traceback (most recent call last):
 File "D:\Python\lib\runpy.py", line 162, in _run_module_as_main
  "__main__", fname, loader, pkg_name)
 File "D:\Python\lib\runpy.py", line 72, in _run_code 
  exec code in run_globals
 File "D:\Python\Scripts\pip.exe\__main__.py", line 5, in <module>
ImportError: cannot import name main

采纳答案by Wan Bachtiar

Even though the original question seems to be from 2015, this 'bug' seems to affect users installing pip-10.0.0as well.

尽管最初的问题似乎来自 2015 年,但这个“错误”似乎也影响了用户的安装pip-10.0.0

The workaround is not to modify pip, however to change the way pip is called. Instead of calling /usr/bin/pipcall pipvia Python itself. For example, instead of the below:

解决方法不是修改pip,而是更改调用 pip 的方式。而不是通过 Python 本身调用/usr/bin/pipcall pip。例如,而不是下面的:

pip install <package>

If from Python version 2 (or default Python binary is called python) do :

如果从 Python 版本 2(或调用默认 Python 二进制文件python)开始:

python -m pip install <package>

or if from Python version 3:

或者如果来自 Python 版本 3:

python3 -m pip install <package> 

回答by catalinpopescu

The bug is found in pip 10.0.0.

该错误在 pip 10.0.0 中发现。

In linux you need to modify file: /usr/bin/pip from:

在 linux 中,您需要修改文件:/usr/bin/pip 从:

from pip import main
if __name__ == '__main__':
    sys.exit(main())

to this:

对此:

from pip import __main__
if __name__ == '__main__':
    sys.exit(__main__._main())

回答by Madis N?mme

On MacOS if you've installed python via Homebrew, change the line in /usr/local/opt/python/libexec/bin/pip

在 MacOS 上,如果您通过 Homebrew 安装了 python,请更改 /usr/local/opt/python/libexec/bin/pip

from

from pip.internal import main

to

from pip._internal import main

Or use this one liner: sed -i '' "s/from pip import main/from pip._internal import main/" /usr/local/opt/python/libexec/bin/pip

或者使用这个衬垫: sed -i '' "s/from pip import main/from pip._internal import main/" /usr/local/opt/python/libexec/bin/pip

Explanation:

解释:

The issue is caused by the changes in pip version 10 moving internal namespace under main._internaland the bin script put in place by homebrew still looking it from the old place (where it used to be in version 9). Issue and some discussion https://github.com/pypa/pip/issues/5240

该问题是由 pip 版本 10 中的更改移动内部命名空间引起的,main._internal并且自制软件放置的 bin 脚本仍然从旧位置(它曾经在版本 9 中)查看它。问题和一些讨论https://github.com/pypa/pip/issues/5240

回答by Jacob Geryk

For those having similar trouble using pip 10 with PyCharm, download the latest versionhere

对于那些在使用 pip 10 和 PyCharm 时遇到类似问题的人,请在此处下载最新版本

回答by Martín De la Fuente

If you have a hardlink to pip in your PATH(i.e. if you have multiple python versions installed) and then you upgrade pip, you may also encounter this error.

如果您的 pip 中有硬链接PATH(即如果您安装了多个 python 版本),然后升级 pip,您也可能会遇到此错误。

The solution consists in creating the hardlink again. Or even better, stop using hardlinks and use softlinks.

解决方案包括再次创建硬链接。或者更好的是,停止使用硬链接并使用软链接。

回答by Shuai Liu

On Ubuntu Server 16, I have the same problem with python27. Try this:

在 Ubuntu Server 16 上,python27 也有同样的问题。尝试这个:

Change

改变

from pip import main
if __name__ == '__main__':
    sys.exit(main())

To

from pip._internal import main
if __name__ == '__main__':
    sys.exit(main())

回答by Bachir Mehemmel

i fixed the problem by reinstalling pip using get-pip.py.

我通过重新安装 pip 解决了这个问题get-pip.py

  1. Download get-pip from official link: https://pip.pypa.io/en/stable/installing/#upgrading-pip
  2. run it using commande: python get-pip.py.
  1. 从官方链接下载 get-pip:https: //pip.pypa.io/en/stable/installing/#upgrading-pip
  2. 使用 commande: 运行它python get-pip.py

And pip is fixed and work perfectly.

并且 pip 是固定的并且可以完美运行。

回答by kiyah

On Windows 10, I used the following commands to downgrade pip:

在 Windows 10 上,我使用以下命令降级 pip:

python -m pip uninstall pip
python -m pip install pip==9.0.3

This should also work on Linux and Mac too.

这也应该适用于 Linux 和 Mac。

回答by Anas Musah

It works on ubuntu 16.04. Step 1:

它适用于 ubuntu 16.04。第1步:

 sudo gedit /home/user_name/.local/bin/pip

a file opens with the content:

一个文件打开,内容如下:

#!/usr/bin/python

# -*- coding: utf-8 -*-
import re
import sys

from pip import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

Change the mainto __main__as it appears below:

更改main__main__下方显示:

#!/usr/bin/python

# -*- coding: utf-8 -*-
import re
import sys

from pip import __main__

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(__main__._main())

Save the file and close it. And you are done!

保存文件并关闭它。你已经完成了!

回答by Abdul Gaffar

try this

尝试这个

#!/usr/bin/python
# GENERATED BY DEBIAN

import sys

# Run the main entry point, similarly to how setuptools does it, but because
# we didn't install the actual entry point from setup.py, don't use the
# pkg_resources API.i
try:
    from pip import main
except ImportError:
    from pip._internal import main
if __name__ == '__main__':
    sys.exit(main())