Python 升级 pip 后出错:无法导入名称“main”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49836676/
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
Error after upgrading pip: cannot import name 'main'
提问by g_p
Whenever I am trying to install any package using pip, I am getting this import error:
每当我尝试使用 pip 安装任何软件包时,都会收到此导入错误:
guru@guru-notebook:~$ pip3 install numpy
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
ImportError: cannot import name 'main'
guru@guru-notebook:~$ cat `which pip3`
#!/usr/bin/python3
# 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.
from pip import main
if __name__ == '__main__':
sys.exit(main())
It was working fine earlier, I am not sure why it is throwing this error. I have searched about this error, but can't find anything to fix it.
早些时候它工作正常,我不知道为什么会抛出这个错误。我已经搜索过这个错误,但找不到任何可以修复它的东西。
Please let me know if you need any further detail, I will update my question.
如果您需要任何进一步的细节,请告诉我,我会更新我的问题。
回答by Anthony Sottile
You must have inadvertently upgraded your system pip (probably through something like sudo pip install pip --upgrade
)
您一定是无意中升级了您的系统 pip(可能是通过类似的方式sudo pip install pip --upgrade
)
pip 10.x adjusts where its internals are situated. The pip3
command you're seeing is one provided by your package maintainer (presumably debian based here?) and is not a file managed by pip.
pip 10.x 调整其内部结构的位置。pip3
您看到的命令是由您的包维护者提供的(大概是基于 debian 的?),而不是由 pip 管理的文件。
You can read more about this on pip's issue tracker
您可以在pip 的问题跟踪器上阅读更多相关信息
You'll probably want to notupgrade your system pip and instead use a virtualenv.
你可能会想不升级系统PIP和改为使用的virtualenv。
To recover the pip3
binary you'll need to sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
.
要恢复pip3
二进制文件,您需要sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
.
If you want to continue in "unsupported territory" (upgrading a system package outside of the system package manager), you can probably get away with python3 -m pip ...
instead of pip3
.
如果您想继续在“不受支持的领域”(升级系统包管理器之外的系统包),您可能可以使用python3 -m pip ...
而不是pip3
.
回答by vijay athithya
We can clear the error by modifying the pip file.
我们可以通过修改pip文件来清除错误。
Check the location of the file:
检查文件的位置:
$ which pip
path -> /usr/bin/pip
路径 -> /usr/bin/pip
Go to that location(/usr/bin/pip) and open terminal
转到该位置(/usr/bin/pip)并打开终端
Enter: $ sudo nano pip
进入: $ sudo nano pip
You can see:
你可以看到:
import sys
from pip import main
if __name__ == '__main__':
sys.exit(main())
Change to:
改成:
import sys
from pip import __main__
if __name__ == '__main__':
sys.exit(__main__._main())
then ctrl + owrite the changes and exit
然后ctrl + o写入更改并退出
Hope this will do!!
希望这会成功!!
回答by Alex C.
For Ubuntu family, Debian, Linux Mint users
适用于 Ubuntu 家族、Debian、Linux Mint 用户
Thanks to Anthony's explanation above, you can retain your original system pip (in /usr/bin/ and dist-packages/) and remove the manually-installed pip (in ~/.local/) to resolve the conflict:
感谢 Anthony上面的解释,您可以保留原始系统 pip(在 /usr/bin/ 和 dist-packages/ 中)并删除手动安装的 pip(在 ~/.local/ 中)以解决冲突:
$ python3 -m pip uninstall pip
$ python3 -m pip uninstall pip
Ubuntu/Debian pip v8.1.1 (16.04) from python3-pip
debian package (see$ pip3 -V
) shows the same search results as the latest pip v10.0.1, and installs latest modules from PyPI just fine. It has a working pip
command (already in the $PATH), plus the nice --user
option patched-in by default since 2016. Looking at pip release notes, the newer versions are mostly about use-case specific bug fixes and certain new features, so noteveryone has to rush upgrading pip just yet. And the new pip 10 can be deployed to Python virtualenvs, anyway.
来自python3-pip
debian 包(请参阅参考资料)的 Ubuntu/Debian pip v8.1.1 (16.04)$ pip3 -V
显示与最新的 pip v10.0.1 相同的搜索结果,并且安装来自 PyPI 的最新模块就好了。它有一个工作pip
命令(已经在 $PATH 中),加上--user
自 2016 年以来默认修补的好选项。查看pip 发行说明,较新的版本主要是关于用例特定的错误修复和某些新功能,所以不是每个人都必须急于升级pip。无论如何,新的 pip 10 可以部署到 Python virtualenvs。
But regardless of pips, your OS allows to quickly install common Python modules (including numpy) with APT, without the need for pip, for example:$ sudo apt install python3-numpy python3-scipy
(with system dependencies)$ sudo apt install python3-pip
(Debian-patched pip, slightly older but it doesn't matter)
但是不管 pips,您的操作系统允许使用 APT 快速安装常见的 Python 模块(包括 numpy),而无需 pip,例如:($ sudo apt install python3-numpy python3-scipy
带有系统依赖项)$ sudo apt install python3-pip
(Debian 补丁 pip,稍旧但没关系)
Quick apt syntax reminder (please see
man apt
for details):$ sudo apt update
(to resync Ubuntu package index files from up-to-date sources)$ apt search <python-package-name>
(full text-search on all available packages)$ apt show <python-package-name>
(displays the detailed package description)$ sudo apt install <python-package-name>
快速 apt 语法提示(请参阅
man apt
详细信息):($ sudo apt update
从最新源重新同步 Ubuntu 包索引文件)$ apt search <python-package-name>
(对所有可用包进行全文搜索)$ apt show <python-package-name>
(显示详细的包描述)$ sudo apt install <python-package-name>
Package names prefixed with python-
are for Python 2; and prefixed with python3-
are for Python 3 (e.g. python3-pandas). There are thousands, and they undergo integration testing within Debian and Ubuntu. Unless you seek to install at per-user level (pip install --user
option) or within virtualenv/venv, apt could be what you needed. These system packages are accessible from virtual envs too, as virtualenvwill gracefully fall back to using system libs on import if your envs don't have given copies of modules.
Your custom-installed (with pip --user
) per-user modules in ~/.local/lib
will override them too.
前缀python-
为 Python 2 的包名;和前缀python3-
是 Python 3(例如 python3-pandas)。有数千个,它们在 Debian 和 Ubuntu 中进行了集成测试。除非您寻求在每个用户级别(pip install --user
选项)或在 virtualenv/venv 中安装,否则apt 可能就是您所需要的。这些系统包也可以从虚拟环境访问,因为如果您的环境没有提供模块的副本,virtualenv将优雅地回退到在导入时使用系统库。您自定义安装(使用 pip --user
)的每个用户模块~/.local/lib
也将覆盖它们。
Note, since this is a system-wideinstallation, you'd rarely need to remove them (need to be mindful about OS dependencies). This is convenient for packages with many system dependencies (such as with scipy or matplotlib), as APT will keep track and provide all required system libs and C extensions, while with pip you have no such guarantees.
请注意,由于这是系统范围的安装,因此您很少需要删除它们(需要注意操作系统依赖性)。这对于具有许多系统依赖项(例如使用 scipy 或 matplotlib)的包很方便,因为 APT 将跟踪并提供所有必需的系统库和 C 扩展,而使用 pip 则没有这样的保证。
In fact, for system-wide Python packages (in contrast to per-user, home dir level, or lower), Ubuntu expectsusing the APT package manager (rather than sudo pip
) to avoid breaking OS: sudo pip3
targets the very same /usr/lib/python3/dist-packages
directory where APT stores OS-sensitive modules. Recent Debian/Ubuntu releases depend heavily on Python 3, so its pre-installed modules are managed by apt
and shouldn't be changed.
事实上,对于系统范围的 Python 包(与每个用户、主目录级别或更低级别相比),Ubuntu期望使用 APT 包管理器(而不是sudo pip
)来避免破坏操作系统:sudo pip3
目标与/usr/lib/python3/dist-packages
APT 存储操作系统的目录完全相同- 敏感模块。最近的 Debian/Ubuntu 版本严重依赖 Python 3,因此其预安装的模块由管理apt
且不应更改。
So if you use pip3 install
command, please ensure that it runs in an isolated virtual dev environment, such as with virtualenv(sudo apt install python3-virtualenv
), or with Python3 built-in (-m venv
), or at a per-user level (--user
pip option, default in Ubuntu-provided pip since 2016), but notsystem-wide (never sudo pip3
!), because pip interfereswith the operation of the APT package manager and may affect Ubuntu OS componentswhen a system-used python module is unexpectedly changed. Good luck!
因此,如果您使用pip3 install
命令,请确保它在隔离的虚拟开发环境中运行,例如使用virtualenv( sudo apt install python3-virtualenv
),或使用 Python3 内置 ( -m venv
),或在每个用户级别(--user
pip 选项,Ubuntu 提供的默认值) pip 自 2016 年以来),但不是系统范围的(永远不会sudo pip3
!),因为 pip 会干扰APT 包管理器的操作,并且当系统使用的 python 模块意外更改时,可能会影响 Ubuntu 操作系统组件。祝你好运!
P.S.All the above is for the 'ideal' solution (Debian/Ubuntu way).
P. S.以上所有内容都是针对“理想”解决方案(Debian/Ubuntu 方式)。
If you stillwant to use the new pip3 v10 exclusively, there are 3 quick workarounds:
如果您仍想单独使用新的 pip3 v10,有 3 个快速解决方法:
- simply open a new bash session (a new terminal tab, or type
bash
) - and pip3 v10 becomes available (seepip3 -V
). debian's pip3 v8 remains installed but is broken; or - the command
$ hash -d pip3 && pip3 -V
to refresh pip3 pathname in the $PATH. debian's pip3 v8 remains installed but is broken; or - the command
$ sudo apt remove python3-pip && hash -d pip3
to uninstall debian's pip3 v8 completely, in favor of your new pip3 v10.
- 只需打开一个新的 bash 会话(一个新的终端选项卡,或输入
bash
) - pip3 v10 就可用了(请参阅 参考资料pip3 -V
)。debian 的 pip3 v8 仍然安装但已损坏;或者 $ hash -d pip3 && pip3 -V
在 $PATH 中刷新 pip3 路径名的命令。debian 的 pip3 v8 仍然安装但已损坏;或者$ sudo apt remove python3-pip && hash -d pip3
完全卸载 debian 的 pip3 v8的命令,以支持新的 pip3 v10。
Note:You will always need to add --user
flag to any non-debian-provided pip, unless you are in a virtualenv! (it deploys python packages to ~/.local/
, default in debian/ubuntu-provided python3-pip and python-pip since 2016). Your use of pip 10 system-wide, outside of virtualenv, is not really supported by Ubuntu/Debian. Never sudo pip3
!
注意:--user
除非您在 virtualenv 中,否则您将始终需要向任何非 debian 提供的 pip添加标志!(~/.local/
自 2016 年以来,它默认在 debian/ubuntu 提供的 python3-pip 和 python-pip 中部署 python 包)。Ubuntu/Debian 并不真正支持您在 virtualenv 之外在系统范围内使用 pip 10。从来没有sudo pip3
!
Further details:
https://github.com/pypa/pip/issues/5221#issuecomment-382069604
https://github.com/pypa/pip/issues/5240#issuecomment-381673100
更多详情:
https: //github.com/pypa/pip/issues/5221#issuecomment-382069604
https://github.com/pypa/pip/issues/5240#issuecomment-381673100
回答by Saket
resolved in one step only.
一步解决。
I too faced this issue, But this can be resolved simply by 1 command without bothering around and wasting time and i have tried it on multiple systems it's the cleanest solution for this issue. And that's:
我也遇到过这个问题,但这可以通过 1 个命令简单地解决,而无需费心和浪费时间,我已经在多个系统上尝试过,这是解决此问题的最干净的解决方案。那就是:
For python3:- sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
.
对于python3:- sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
。
By this , you can simply install packages using pip3
. to check use pip3 --version
.
通过这种方式,您可以简单地使用pip3
. 检查使用pip3 --version
。
For older versions, use : sudo python -m pip uninstall pip && sudo apt install python-pip --reinstall
.
对于旧版本,请使用 : sudo python -m pip uninstall pip && sudo apt install python-pip --reinstall
。
By this, now you can simply install packages using pip
. to check use pip --version
.
这样,现在您可以简单地使用pip
. 检查使用pip --version
。
回答by Codemaker
You can resolve this issue by reinstalling pip.
您可以通过重新安装 pip 来解决此问题。
Use one of the following command line commands to reinstall pip:
使用以下命令行命令之一重新安装 pip:
Python2:
蟒蛇2:
python -m pip uninstall pip && sudo apt install python-pip --reinstall
Python3:
蟒蛇3:
python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
回答by Sr.PEDRO
Check if pip has been cached on another path, to do so, call $ which pipand check that the path is different from the one prompted in the error, if that's the case run:
检查 pip 是否已缓存在另一条路径上,为此,请调用$ which pip并检查该路径是否与错误中提示的路径不同,如果是这种情况,请运行:
$ hash -r
When the cache is clear, pip will be working again. reference: http://cheng.logdown.com/posts/2015/06/14/-usr-bin-pip-no-such-file-or-directory
当缓存清除后,pip 将再次工作。参考:http: //cheng.logdown.com/posts/2015/06/14/-usr-bin-pip-no-such-file-or-directory
回答by Scott Mermelstein
I'm running on a system where I have sudo apt but no sudo pip. (And no su access.) I got myself into this same situation by following the advice from pip:
我在一个有 sudo apt 但没有 sudo pip 的系统上运行。(并且没有 su 访问权限。)我按照 pip 的建议让自己陷入了同样的境地:
You are using pip version 8.1.1, however 18.0 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
您使用的是 pip 版本 8.1.1,但 18.0 可用。您应该考虑通过“pip install --upgrade pip”命令进行升级。
None of the other fixes worked for me, because I don't have enough admin privileges. However, a few things stuck with me from reading up on this:
其他修复程序都不适合我,因为我没有足够的管理员权限。然而,在阅读这篇文章时,有一些事情困扰着我:
- I shouldn't have done this. Sure, pip told me to. It lied.
- Using --user solves a lot of issues by focusing on the user-only directory.
- 我不应该这样做。当然,pip 告诉我的。它说谎了。
- 使用 --user 通过专注于 user-only 目录解决了很多问题。
So, I found this command line to work to revert me back to where I was. If you were using a different version than 8.1.1, you will obviously want to change that part of the line.
所以,我发现这个命令行可以让我恢复到原来的状态。如果您使用的是与 8.1.1 不同的版本,您显然希望更改该行的那部分。
python -m pip install --force-reinstall pip==8.1.1 --user
That's the only thing that worked for me, but it worked perfectly!
这是唯一对我有用的东西,但它完美无缺!
回答by Claire
Recover with python3 -m pip install --user pip==9.0.1
(or the version that worked)
恢复python3 -m pip install --user pip==9.0.1
(或有效的版本)
回答by fliptopbox
Same thing happened to me on Pixelbook using the new LXC (strech). This solution is very similar to the accepted one, with one subtle difference, whiched fixed pip3 for me.
我在使用新的 LXC(strech)的 Pixelbook 上也发生了同样的事情。这个解决方案与公认的解决方案非常相似,有一个细微的区别,它为我修复了 pip3。
sudo python3 -m pip install --upgrade pip
That bumped the version, and now it works as expected.
这影响了版本,现在它按预期工作。
I found it here ... Python.org: Ensure pip is up-to-date
我在这里找到它... Python.org:确保 pip 是最新的
回答by jkjung13
I met the same problem on my Ubuntu 16.04 system. I managed to fix it by re-installing pip with the following command:
我在 Ubuntu 16.04 系统上遇到了同样的问题。我设法通过使用以下命令重新安装 pip 来修复它:
curl https://bootstrap.pypa.io/get-pip.py | sudo python3
curl https://bootstrap.pypa.io/get-pip.py | sudo python3