【问题标题】:pip3 --version ImportErrorpip3 --version 导入错误
【发布时间】:2017-10-17 18:59:20
【问题描述】:

pip3 --version 最近抛出了一个错误,我在使用 pip 将软件包安装到我的虚拟环境时遇到了问题。这是一个新问题,但我认为这可能是由于我的计算机上安装了太多 python 版本。

有其他人看到这个错误吗?我以前从未遇到过 importlib.util 引发的错误。此外,这个错误最近才出现。据我所知,我没有对 importlib.util 进行任何更改。

Error processing line 1 of /usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib-2.0.2-py3.6-nspkg.pth:

  Traceback (most recent call last):
    File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site.py", line 168, in addpackage
      exec(line)
    File "<string>", line 1, in <module>
  AttributeError: module 'importlib.util' has no attribute 'module_from_spec'

Remainder of file ignored
Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 11, in <module>
    load_entry_point('pip==9.0.1', 'console_scripts', 'pip3')()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 561, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2631, in load_entry_point
    return ep.load()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2291, in load
    return self.resolve()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2297, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/usr/local/lib/python3.6/site-packages/pip/__init__.py", line 26, in <module>
    from pip.utils import get_installed_distributions, get_prog
  File "/usr/local/lib/python3.6/site-packages/pip/utils/__init__.py", line 22, in <module>
    from pip.compat import console_to_str, expanduser, stdlib_pkgs
  File "/usr/local/lib/python3.6/site-packages/pip/compat/__init__.py", line 60, in <module>
    from importlib.util import cache_from_source
ImportError: cannot import name 'cache_from_source'

【问题讨论】:

    标签: python-3.x pip


    【解决方案1】:

    我确定 'module_from_spec' 存在于 3.6 中。您实际上可以指定调用 pip 的确切版本,请尝试一下:

    pip3.6 install packagename
    

    【讨论】:

    • 谢谢你,我会在我回到我的电脑前尝试。我相信我尝试 pip3.6 --version 只是为了查看返回的内容,但不记得是否重现了错误。虽然看起来“cache_from_source”也有问题。这与“module_from_spec”有关吗?
    • 我可以确认,当我运行 pip3 --version 时,我得到了同样的错误,当我尝试安装到虚拟环境时,我遇到了类似的问题。
    • 在使用 PATH 并删除文件 'matplotlib-2.0.2-py3.6-nspkg.pth 后,我能够运行 pip3 --version 而没有错误。我不再收到importlib.util has no attribute 'module_from_spec。但是当我尝试将 pip3 安装到虚拟环境时,我在底部得到与以前相同的错误:from importlib.util import cache_from_source ImportError: cannot import name 'cache_from_source'
    【解决方案2】:

    我已经找到了解决这个错误的方法,但目前还没有确定它意外出现的原因。我怀疑这是因为我的计算机上安装了多个 python(不同的版本以及从不同位置下载的,如自制软件、anaconda、OSX 发行版等)。

    请注意,安装不同的原因不仅是为了安装更新版本的 python,还因为在我学习 python 期间,我参加了很多课程,通常会建议特定的安装方法。

    在查看 importlib.util 时,代码中没有任何明显错误,因此我决定查看 python 的 anaconda 安装(均为 python 3.6)并比较 importlib.util 文件。

    引发错误的 importlib.util 文件的顶部如下所示:

    """Utility code for constructing importers, etc."""
    import functools
    import sys
    import types
    import warnings
    from contextlib import contextmanager
    
    from . import abc
    from ._bootstrap import _find_spec
    from ._bootstrap import _resolve_name
    

    然而,anaconda 版本的 importlib.util 文件的顶部看起来像这样:

    """Utility code for constructing importers, etc."""
    from . import abc
    from ._bootstrap import module_from_spec
    from ._bootstrap import _resolve_name
    from ._bootstrap import spec_from_loader
    from ._bootstrap import _find_spec
    from ._bootstrap_external import MAGIC_NUMBER
    from ._bootstrap_external import cache_from_source
    from ._bootstrap_external import decode_source
    from ._bootstrap_external import source_from_cache
    from ._bootstrap_external import spec_from_file_location
    
    from contextlib import contextmanager
    import functools
    import sys
    import types
    import warnings
    

    使用 IntelliJ,我可以确认这两个文件没有其他差异。

    注意到从 ._bootstrap_external 导入的差异,我将 anaconda importlib.util 文件中的以下行复制并粘贴到 usr/bin importlib.util 文件中:

    from ._bootstrap_external import MAGIC_NUMBER
    from ._bootstrap_external import cache_from_source
    from ._bootstrap_external import decode_source
    from ._bootstrap_external import source_from_cache
    from ._bootstrap_external import spec_from_file_location
    from ._bootstrap import spec_from_loader
    from ._bootstrap import module_from_spec
    

    保存后,pip3 功能恢复。我尚未确定最初导致更改的原因,但如果其他人有类似的问题,我建议从上面的第三个块中复制代码并将其插入到 importlib.util 文件的顶部。

    【讨论】:

      猜你喜欢
      • 2018-09-11
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2021-01-05
      • 1970-01-01
      相关资源
      最近更新 更多