2017-10-17 208 views
0

pip3 --version最近发生错误,我无法使用pip将软件包安装到我的虚拟环境中。这是一个新问题,但我认为这可能是由于我的电脑上安装了太多的python版本。pip3 --version ImportError

有没有人看过这个错误?我从来没有从以前抛出的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' 

回答

1

我确定'module_from_spec'存在于3.6中。实际上,你可以指定你的电话点子上确切的版本,请给一个尝试:

pip3.6 install packagename 
+0

谢谢你,当我回到我的电脑时,我会尝试。我相信我尝试过pip3.6 - 只是为了查看返回的内容,但如果错误被复制,则无法回想。虽然它看起来像'cache_from_source'也有问题。这与'module_from_spec'有关吗? – Hanzy

+0

我可以确认,当我运行pip3 --version时,我得到了相同的错误,当试图安装到虚拟环境时,我得到了类似的问题。 – Hanzy

+0

使用PATH并删除文件'matplotlib-2.0.2-py3.6-nspkg.pth后,我能够正常运行pip3 --version。我不再得到'importlib.util没有'module_from_spec'属性。 但是当我尝试将pip3安装到虚拟环境时,我在底部获得与以前相同的错误: 'from importlib.util import cache_from_source ImportError:无法导入名称'cache_from_source' ' – Hanzy

0

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

请注意,不同安装的原因不仅是安装python的更新版本,而且还因为在我的python教育期间,我采取了许多类,通常建议一种特定的安装方法。

查看importlib.util时,代码中没有任何明显错误,因此我决定查看python(包括python 3.6)的anaconda安装并比较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 

然而,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进口的区别我复制并粘贴从蟒蛇以下行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文件的顶部。