ImportError:在Windows 7 32位中运行pip –version命令时无法导入名称main

问题:ImportError:在Windows 7 32位中运行pip –version命令时无法导入名称main

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

这是pip --version在Administrator 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

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.

Here’s the error after running pip --version in Administrator 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

回答 0

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

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

pip install <package>

如果来自Python版本2(或称为默认Python二进制文件python),请执行以下操作:

python -m pip install <package>

还是从Python版本3:

python3 -m pip install <package> 

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

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

pip install <package>

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

python -m pip install <package>

or if from Python version 3:

python3 -m pip install <package> 

回答 1

该错误位于pip 10.0.0中。

在Linux中,您需要从以下位置修改文件:/ usr / bin / pip:

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

对此:

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

The bug is found in pip 10.0.0.

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

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

to this:

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

回答 2

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

更改

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

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

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

Change

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

To

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

回答 3

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

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

这也应该在Linux和Mac上也可以使用。

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

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

This should also work on Linux and Mac too.


回答 4

我遇到了同样的问题,但是使用apt和pip卸载并重新安装对我来说不起作用。

我看到了另一个解决方案,它提供了一种恢复pip3路径的简便方法:

sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall

I had the same problem, but uninstall and reinstall with apt and pip didn’t work for me.

I saw another solution that presents a easy way to recover pip3 path:

sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall

回答 5

我通过使用重新安装pip来解决此问题get-pip.py

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

点子是固定的,可以完美地工作。

i fixed the problem by reinstalling pip using 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.

And pip is fixed and work perfectly.


回答 6

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

from pip.internal import main

from pip._internal import main

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

说明:

此问题是由于pip版本10中的内部移动命名空间的更改main._internal以及Homebrew仍然从旧位置(以前是版本9中)查找的bin脚本引起的。问题和一些讨论https://github.com/pypa/pip/issues/5240

On MacOS if you’ve installed python via Homebrew, change the line in /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

Explanation:

The issue is caused by the changes in pip version 10 moving internal namespace under main._internal and 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


回答 7

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

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

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.

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


回答 8

在Windows 10上,我遇到了同样的问题。PIP 19已安装在我的系统中,但没有显示。错误是No Module Found

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

降级pip到9.0.3对我来说很好。

On Windows 10, I had the same problem. PIP 19 was already installed in my system but wasn’t showing up. The error was No Module Found.

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

Downgrading pip to 9.0.3 worked fine for me.


回答 9

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

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


回答 10

它适用于Ubuntu 16.04。第1步:

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

将打开一个包含以下内容的文件:

#!/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改为__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())

保存文件并关闭它。您完成了!

It works on ubuntu 16.04. Step 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 main to __main__ as it appears below:

#!/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!


回答 11

试试这个

#!/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())

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())

回答 12

一个适用于Ubuntu的简单解决方案,但也可以在Windows上解决该问题:

刚打电话

pip install --upgrade pip

A simple solution that works with Ubuntu, but may fix the problem on windows too:

Just call

pip install --upgrade pip

回答 13

这在尝试使用python3.6时解决了我在ubuntu 18.04中的问题:

rm -rf ~/.local/lib/python3.6

您可以使用mv将文件夹移动到另一个位置,而不是将其删除,以进行测试:

mv ~/.local/lib/python3.6 ./python3.6_old

This solved my problem in ubuntu 18.04 when trying to use python3.6:

rm -rf ~/.local/lib/python3.6

You can move the folder to another place using mv instead of deleting it too, for testing:

mv ~/.local/lib/python3.6 ./python3.6_old

回答 14

打开您的终端Linux。

hash -d pip

Open your terminal linux.

hash -d pip

回答 15

在我们的案例中,在2020年使用Python3,解决此问题的方法是将Python安装移至cloud-init实例化VM 的启动脚本。

当我们尝试使用VM生命周期中稍后由用户调用的脚本来安装Python时,我们也遇到了相同的错误,但是将相同的Python安装代码移至该cloud-init脚本可以解决此问题。

In our case, in 2020 using Python3, the solution to this problem was to move the Python installation to the cloud-init startup script which instantiated the VM.

We had been encountering this same error when we had been trying to install Python using scripts that were called by users later in the VM’s life cycle, but moving the same Python installation code to the cloud-init script eliminated this problem.