博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
升级pip后出错:无法导入名称“ main”
阅读量:2290 次
发布时间:2019-05-09

本文共 9922 字,大约阅读时间需要 33 分钟。

本文翻译自:

Whenever I am trying to install any package using pip, I am getting this import error: 每当我尝试使用pip安装任何软件包时,都会收到此导入错误:

guru@guru-notebook:~$ pip3 install numpyTraceback (most recent call last):  File "/usr/bin/pip3", line 9, in 
from pip import mainImportError: cannot import name 'main'

guru@guru-notebook:~$ cat `which pip3`#!/usr/bin/python3# GENERATED BY DEBIANimport 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 mainif __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. 如果您需要更多详细信息,请告诉我,我将更新我的问题。


#1楼

参考:


#2楼

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. pip 10.x调整其内部位置。 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. 您看到的pip3命令是您的软件包维护者提供的(大概是在这里基于debian?),而不是pip管理的文件。

You can read more about this on 您可以在上了解有关此内容的更多信息

You'll probably want to not upgrade 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


#3楼

Check if pip has been cached on another path, to do so, call $ which pip and 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. 清除缓存后,pip将再次起作用。 reference: 参考: :


#4楼

I use sudo apt remove python3-pip then pip works. 我使用sudo apt remove python3-pip然后pip可以工作。

~ sudo pip install pip --upgrade[sudo] password for sen: Traceback (most recent call last):  File "/usr/bin/pip", line 9, in 
from pip import mainImportError: cannot import name 'main'➜ ~ sudo apt remove python3-pip Reading package lists... DoneBuilding dependency tree Reading state information... DoneThe following packages were automatically installed and are no longer required: libexpat1-dev libpython3-dev libpython3.5-dev python-pip-whl python3-dev python3-wheel python3.5-devUse 'sudo apt autoremove' to remove them.The following packages will be REMOVED: python3-pip0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.After this operation, 569 kB disk space will be freed.Do you want to continue? [Y/n] y(Reading database ... 215769 files and directories currently installed.)Removing python3-pip (8.1.1-2ubuntu0.4) ...Processing triggers for man-db (2.7.5-1) ...➜ ~ pipUsage: pip
[options]

#5楼

For Ubuntu family, Debian, Linux Mint users 对于Ubuntu系列,Debian和Linux Mint用户

Thanks to Anthony's explanation , 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

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. python3-pip debian软件包中的Ubuntu / Debian pip v8.1.1(16.04)(请参见$ pip3 -V )显示的搜索结果与最新的pip v10.0.1相同,并且可以从PyPI安装最新的模块。 It has a working pip command (already in the $PATH), plus the nice --user option patched-in by default since 2016. Looking at , the newer versions are mostly about use-case specific bug fixes and certain new features, so not everyone has to rush upgrading pip just yet. 它具有有效的pip命令(已在$ PATH中),以及自2016年以来默认修补的nice --user选项。查看 ,较新的版本主要是针对用例特定的错误修复以及某些新的功能,因此并不是每个人都必须赶紧升级pip。 And the new pip 10 can be deployed to Python virtualenvs, anyway. 无论如何,新的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: 但是,无论使用哪种pip,您的操作系统都可以使用APT快速安装常见的Python模块(包括numpy),而无需使用pip,例如:

$ sudo apt install python3-numpy python3-scipy (with system dependencies) $ sudo apt install python3-numpy python3-scipy (具有系统依赖性)
$ sudo apt install python3-pip (Debian-patched pip, slightly older but it doesn't matter) $ sudo apt install python3-pip (Debian修补的pip,稍旧,但是没关系)

Quick apt syntax reminder (please see man apt for details): 快速apt语法提示(请参阅man apt的详细信息):

$ sudo apt update (to resync Ubuntu package index files from up-to-date sources) $ sudo apt update (从最新资源重新同步Ubuntu软件包索引文件)
$ apt search <python-package-name> (full text-search on all available packages) $ apt search <python-package-name> (对所有可用软件包进行全文搜索)
$ apt show <python-package-name> (displays the detailed package description) $ apt show <python-package-name> (显示详细的软件包描述)
$ sudo apt install <python-package-name>

Package names prefixed with python- are for Python 2; 带有python-前缀的软件包名称适用于Python 2; and prefixed with python3- are for Python 3 (eg python3-pandas). 并以python3-作为前缀用于Python 3(例如python3-pandas)。 There are thousands, and they undergo integration testing within Debian and Ubuntu. 有成千上万个,它们在Debian和Ubuntu中进行集成测试。 Unless you seek to install at per-user level ( pip install --user option) or within virtualenv/venv, apt could be what you needed. 除非您寻求在每个用户级别( pip install --user选项)或在virtualenv / venv内进行pip install --user ,否则可能会需要apt。 These system packages are accessible from virtual envs too, as will 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. 您在~/.local/lib自定义安装(使用pip --user )的每个用户模块也将覆盖它们。

Note, since this is a installation, you'd rarely need to remove them (need to be mindful about OS dependencies). 请注意,由于这是安装,因此您几乎不需要删除它们(需要注意OS依赖性)。 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 . 这对于具有许多系统依赖性的软件包(例如,使用scipy或matplotlib)很方便,因为APT会跟踪并提供所有必需的系统库和C扩展名,而使用pip则 。

In fact, for system-wide Python packages (in contrast to per-user, home dir level, or lower), Ubuntu using 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. 实际上,对于系统范围的Python软件包(与每个用户,家庭目录级别或更低版本相反),Ubuntu 使用APT软件包管理器(而不是sudo pip )来避免破坏操作系统: sudo pip3目标与/usr/lib/python3/dist-packages目录,APT在此目录中存储操作系统敏感的模块。 Recent Debian/Ubuntu releases depend heavily on Python 3, so its pre-installed modules are managed by apt and shouldn't be changed. 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 ( 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 not system-wide (never sudo pip3 !), because pip with the operation of the APT package manager and may affect Ubuntu OS when a system-used python module is unexpectedly changed. 因此,如果您使用pip3 install命令,请确保它在隔离的虚拟开发环境中运行,例如 ( sudo apt install python3-virtualenv ),Python3内置( -m venv )或每个用户级别( --user自2016 PIP选项,默认在Ubuntu提供的PIP),而不是全系统(从不sudo pip3 !),因为PIP 与APT包管理器的操作,并可能影响Ubuntu的操作系统时,系统使用的python模块意外更改。 Good luck! 祝好运!


P. S. All the above is for the 'ideal' solution (Debian/Ubuntu way). P. S.以上都是针对“理想”解决方案的(Debian / Ubuntu方式)。

If you still want 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 (see pip3 -V ). 只需打开一个新的bash会话(一个新的终端选项卡,或键入bash )-即可使用pip3 v10(请参阅pip3 -V )。 debian's pip3 v8 remains installed but is broken; debian的pip3 v8仍然安装但已损坏; or 要么
  • the command $ hash -d pip3 && pip3 -V to refresh pip3 pathname in the $PATH. 命令$ hash -d pip3 && pip3 -V刷新$ PATH中的pip3路径名。 debian's pip3 v8 remains installed but is broken; debian的pip3 v8仍然安装但已损坏; 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. 命令$ 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! 注意:除非您处于virtualenv中,否则您将始终需要将--user标志添加到任何非debian提供的pip中! (it deploys python packages to ~/.local/ , default in debian/ubuntu-provided python3-pip and python-pip since 2016). (自2016年以来,它将python软件包部署到~/.local/ ,默认为debian / ubuntu提供的python3-pip和python-pip)。 Your use of pip 10 system-wide, outside of virtualenv, is not really supported by Ubuntu/Debian. Ubuntu / Debian并不真正支持您在virtualenv之外使用系统范围内的pip 10。 Never sudo pip3 ! 永远不要sudo pip3

Further details: 更多详情:


#6楼

I met the same problem on my Ubuntu 16.04 system. 我在Ubuntu 16.04系统上遇到了相同的问题。 I managed to fix it by re-installing pip with the following command: 我设法通过使用以下命令重新安装pip来修复它:

curl https://bootstrap.pypa.io/get-pip.py | sudo python3

转载地址:http://nvcnb.baihongyu.com/

你可能感兴趣的文章