Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name 'metadata' from 'importlib'

Under a python (Python 3.7.5 (default, Oct 31 2019, 15:18:51) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32) session launched in an Anaconda prompt, I get the error

>>> import nbconvert
Traceback (most recent call last):
  File "C:\Users\user1\Anaconda\lib\site-packages\jsonschema\__init__.py", line 31, in <module>
    from importlib import metadata
ImportError: cannot import name 'metadata' from 'importlib' (C:\Users\user1\Anaconda\lib\importlib\__init__.py)

Effectively, metadata is not in importlib

>>> import importlib
>>> dir(importlib)
['_RELOADING', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__import__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_bootstrap', '_bootstrap_external', '_imp', '_r_long', '_w_long', 'abc', 'find_loader', 'import_module', 'invalidate_caches', 'machinery', 'reload', 'sys', 'types', 'util', 'warnings']

This is the only reference I found, mentioning it was observed in some cases (weird?) in python 3.8.


EDIT 1: I am now able to remove the error. There is one change that does that: removing a string in the PYTHONPATH environment variable that led to an OSError: [WinError 123] ... after >>> import nbconvert. Removing / adding that string, removes / reinstates the error.

I am not certain if the fact that I did conda install nbconvert in an activated virtualenv (where I have python 3.8.0) also played a role; I did not test before this install.

More importantly, I cannot figure out the connection between the OSError and the presence of a line from importlib import metadata.


EDIT 2: I have a virtualenv with python 3.8.0, where importlib does not have metadata either. So I still cannot figure out why the presence of a line from importlib import metadata.

C:\> conda activate opencv
C:\> conda list
...
importlib_metadata        1.1.0                    py38_0    defaults
...
nbconvert                 5.6.1                    py38_0    defaults
...
C:\> python
Python 3.8.0 (default, Nov  6 2019, 16:00:02) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib
>>> dir(importlib)
['_RELOADING', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__import__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_bootstrap', '_bootstrap_external', '_imp', '_pack_uint32', '_unpack_uint32', 'find_loader', 'import_module', 'invalidate_caches', 'reload', 'sys', 'types', 'warnings']
>>> import nbconvert
>>>
like image 623
sancho.s ReinstateMonicaCellio Avatar asked Dec 06 '19 15:12

sancho.s ReinstateMonicaCellio


Video Answer


4 Answers

The issue was caused by a recent change in the library jsonschema. Looking at https://github.com/Julian/jsonschema/blob/master/jsonschema/init.py you'll see that there was a change on September 26, 2019 changing pkg_resources to importlib_metadata as a fallback for Python <3.8. However, this doesn't seem to work out of the box.

To fix the issue, you have to downgrade your jsonschema package to a version that predates the change:

pipenv install jsonschema==3.0.2

More information can be found here: https://blog.gaborschulz.com/my-jupyter-notebook-stopped-working.html

like image 109
Jane Kathambi Avatar answered Oct 22 '22 03:10

Jane Kathambi


Today, I ran into an error similar (but not exactly the same) to yours.

Starting from Python 3.8, the importlib module has a metadata submodule. For libraries running under older Python versions, the library importlib_metadata has been made to replicate the behaviour. For example, the jsonschema library (used by Jupyter notebooks), uses it as follows:

# __init__.py from jsonschema 3.2.0
try:
    from importlib import metadata
except ImportError: # for Python<3.8
    import importlib_metadata as metadata
__version__ = metadata.version("jsonschema")

To answer your first question, the cause of the error is most likely due to a mismatch in the versions of the installed libraries you were using. This could a result of manual pip installs or perhaps other libraries failing to list proper dependency versions.

Your second question (edit 1): importing nbconvert triggers the import of jsonschema, which will lead to the same error.

You also mention an OSError, but don't give any details when/how it occurred. In my original problem (how I found your question), I found that the importlib_metadata library could throw an OSError when some user folders are not accessible due to permissions. They fixed this in version 1.4.

Your last question (edit 2): you are using dir(), which lists the attributes of importlib, however, importlib.metadata is a valid module that is simply not listed as an attribute. It can be imported in python 3.8:

Python 3.8.1 (default, Jan  8 2020, 15:55:49) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> dir('importlib')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> from importlib import metadata
>>> dir(metadata)
['ConfigParser', 'Distribution', 'DistributionFinder', 'EntryPoint', 'FileHash', 'MetaPathFinder', 'MetadataPathFinder', 'PackageNotFoundError', 'PackagePath', 'PathDistribution', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'abc', 'collections', 'csv', 'distribution', 'distributions', 'email', 'entry_points', 'files', 'functools', 'import_module', 'io', 'itertools', 'metadata', 'operator', 'os', 'pathlib', 're', 'requires', 'starmap', 'suppress', 'sys', 'version', 'zipfile']
>>>
like image 35
DieterDP Avatar answered Oct 22 '22 01:10

DieterDP


I had the same issue of json schema while launching the jyputer notebook. Indeed this issue is owing to the lastest update in jsonschema. By downgrading the jsonshema to 3.0.2 the error ImportError: cannot import name 'metadata' from 'importlib' resolved and finally the jyputer notebook and jyputer lab launched.

like image 1
Bilal Chandio Avatar answered Oct 22 '22 03:10

Bilal Chandio


how is it going?

I fixed this error using the code:

conda install -c anaconda importlib-metadata

I got it from here: codenong

like image 1
VictorSaraivaRocha Avatar answered Oct 22 '22 03:10

VictorSaraivaRocha