2017-10-17 147 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文件的頂部。