Reputation: 414
pip3 --version is throwing an error as of late and I'm having trouble using pip to install packages to my virtual environment. This is a new issue but I think it could be due to having too many python versions installed on my computer.
Has anyone else seen this error thrown? I haven't ever had an error from importlib.util thrown previously. In addition, this error only creeped up lately. To my knowledge I haven't done anything to change 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'
Upvotes: 1
Views: 4008
Reputation: 414
I have found a solution to this error, but at present still have not determined the cause for it arising unexpectedly. I suspect it's because I have several installations of python on my computer (different versions as well as downloaded from different locations such as homebrew, anaconda, OSX shipped version, etc...).
Note that the reason for the different installations was not only to install updated versions of python, but also because during my education on python I've taken many classes, often which advise a specific installation method.
When looking into importlib.util there wasn't anything noticeably wrong in the code, so I decided to look into the anaconda installation of python (both python 3.6) and compare importlib.util files.
The top of the importlib.util file throwing the errors looked like this:
"""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
However, the top of the anaconda version of importlib.util file looked like this:
"""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
Using IntelliJ I could confirm there were no other differences in the two files.
Noting the differences in imports from ._bootstrap_external I copied and pasted the following lines from the anaconda importlib.util file into the usr/bin importlib.util file:
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
After saving, pip3 functionality is restored. I haven't yet determined what caused the change originally but if anyone else has a similar issue I would recommend copying the code from the third block above and inserting it at the top of your importlib.util file.
Upvotes: 1
Reputation: 5081
I'm certain that 'module_from_spec' exists in 3.6. You can actually specify the exact version that your calling pip on, please give that a try :
pip3.6 install packagename
Upvotes: 1