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

sancho.s ReinstateMonicaCellio picture sancho.s ReinstateMonicaCellio · Dec 6, 2019 · Viewed 16.3k times · Source

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
>>>

Answer

Jane Kathambi picture Jane Kathambi · Jul 29, 2020

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