Python`no module pip.__main__;`尝试安装模块时出错

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

Python `no module pip.__main__;` error when trying to install a module

pythonpip

提问by TheHarpoon

I am getting the following error on my Raspberry Pi: No module named pip__main__; 'pip' is a package and cannot be directly executed

我的 Raspberry Pi 出现以下错误: No module named pip__main__; 'pip' is a package and cannot be directly executed

When I type in to the terminal: sudo python3 -m pip install mp3play

当我在终端输入时: sudo python3 -m pip install mp3play

What is causing this and how can I fix it so that I can install the module mp3play?

这是什么原因造成的,我该如何修复它以便我可以安装模块mp3play

采纳答案by mpacer

Pip is not only a standalone executable, it is also a python module.

Pip 不仅是一个独立的可执行文件,它还是一个 Python 模块。

In fact in the python docs it directly recommends using the -msyntax for installing a package using pip.

事实上,在 python 文档中,它直接推荐使用-m使用 pip 安装包的语法。

See https://docs.python.org/3.5/installing/index.html#basic-usage:

请参阅https://docs.python.org/3.5/installing/index.html#basic-usage

The standard packaging tools are all designed to be used from the command line.

The following command will install the latest version of a module and its dependencies from the Python Packaging Index:

python -m pip install SomePackage

标准打包工具均设计为可从命令行使用。

以下命令将从 Python Packaging Index 安装最新版本的模块及其依赖项:

python -m pip install SomePackage

My guess would have been that your system's pip(the executable) was being shadowed by the python2 version of the pipexecutable. But it sounds like you don't have pip (the module) installed such that your python3 executable can find it, so you may need to reinstall pip (the module) specifically.

我的猜测是你的系统pip(可执行文件)被 python2 版本的pip可执行文件遮住了。但听起来您没有安装 pip (模块),以便您的 python3 可执行文件可以找到它,因此您可能需要专门重新安装 pip (模块)。

For that use python3 -m ensurepip(docs for ensurepip) which will install pip if it is not present from the persepctive of your python3 interpreter.

对于该用途python3 -m ensurepipdocs for ensurepip),如果从您的 python3 解释器的角度来看它不存在,它将安装 pip。

The other issue could be that it's finding a file, executable or directory called pipin your current directory, and it is trying to treat that pipas a module, and it is not in fact a module.

另一个问题可能是它正在查找pip当前目录中调用的文件、可执行文件或目录,并试图将其pip视为模块,而实际上它不是模块。

If it's not that I'm not sure. But it is definitely not because pip is not a module.

如果不是我不确定。但这绝对不是因为 pip 不是模块。

回答by BobChao87

pipis a standalone executable. If pipif in your path, you can just execute

pip是一个独立的可执行文件。如果pip在你的路径中,你可以执行

pip install mp3play

If pipis not in your path, then you need to navigate to the directory where pipis located and then execute the above.

如果pip不在你的路径中,那么你需要导航到所在的目录,pip然后执行上面的。

If needed, add sudoto the command.

如果需要,添加sudo到命令中。

The precise error you are encountered is due to pipbeing a package, but -mis used for executing modules.

您遇到的确切错误是由于pip是一个包,但-m用于执行模块。

EDIT: pipalso comes with several helpful alias functions that point to different Python installs. In general, pippoints to your main Python install (the one you enter when simply executing python), pipVwhere Vis a number such as 2or 3adds the install to your primary Python of version V(pip3adds to your python3environment). Finally there is pipV.Swhere Vis the same as before and Sis the subversion. For instance pip3.4installs for Python 3.4.

编辑:pip还附带了几个有用的别名函数,它们指向不同的 Python 安装。通常,pip指向您的主要 Python 安装(您在简单执行时输入的那个python),pipV其中V是一个数字,例如23将安装添加到您的主要 Python 版本Vpip3添加到您的python3环境)。最后还有pipV.S哪里V和以前一样S是颠覆。例如pip3.4为 Python 3.4 安装。

回答by FusterCluck

I had the same problem. I found that an old pip directory was left over from a python 2.7 install, at C:\Users\my-username\pip. This was causing python to try to load pip from there and fail.

我有同样的问题。我发现 python 2.7 安装遗留了一个旧的 pip 目录,位于C:\Users\my-username\pip. 这导致 python 尝试从那里加载 pip 并失败。

I removed that directory and my error has just become No module named pip.

我删除了那个目录,我的错误刚刚变成No module named pip.

I haven't solved the problem yet, but I'm working through it at http://bugs.python.org/issue29586

我还没有解决这个问题,但我正在http://bugs.python.org/issue29586解决它

回答by Djib2011

I had the same issue and none of the previous answers solved it for me.

我遇到了同样的问题,之前的答案都没有为我解决。

The error appeared when I uninstalled and reinstalled python to my PC. As it appears the previous existing version of pip wasn't completely removed and when I was trying to import it with python -m pip install packageit was actually trying to call the previous version.

当我卸载 python 并将其重新安装到我的 PC 时出现错误。看起来以前存在的 pip 版本没有完全删除,当我尝试使用python -m pip install package它导入它时,它实际上是在尝试调用以前的版本。

To solve it first manually deletethe pip folders in the following locations:

要解决它,请先手动删除以下位置的 pip 文件夹:

C:\Users\username\pip
C:\Users\username\AppData\Local\pip
C:\Users\username\AppData\Local\Programs\Python\Python**\lib\site-packages\pip***
C:\Python**\pip

Then download get-pip.py.

然后下载get-pip.py

Finally, navigate to the folder where you downloaded it and run:

最后,导航到您下载它的文件夹并运行:

python get-pip.py

This procedure should reinstall pip and fix the issue.

此过程应重新安装 pip 并解决问题。