explosion / catalogue

Super lightweight function registries for your library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

import 'importlib.metadata' try-catch doesn't work on Jupyter?

svlandeg opened this issue · comments

There's a stackoverflow post that seems to point at a problem with catalogue:

File "C:\Users\user1\AppData\Local\Continuum\anaconda3\envs\py37\lib\site-packages\catalogue.py", line 8, in
import importlib.metadata as importlib_metadata
ModuleNotFoundError: No module named 'importlib.metadata'

Referring to https://github.com/explosion/catalogue/blob/master/catalogue.py#L8, which makes no sense to me as ModuleNotFoundError is a subtype of ImportError and that is properly caught as an exception.

There's a lot of other things going on in those error logs, too, but this caught my attention and wanted to log this here for future reference.

One thing I noticed, going through the code here, is that catalogue doesn't have a __init__.py - should it ?

which makes no sense to me as ModuleNotFoundError is a subtype of ImportError and that is properly caught as an exception.

Yeah, that's weird. I guess we could just use a naked except here, even though that's kinda bad? The main thing we're trying to achieve here is check whether the user is on 3.8 / already has importlib.metadata as a builtin and if not, use the fallback package.

One thing I noticed, going through the code here, is that catalogue doesn't have a init.py - should it ?

It's a single-file module, so it doesn't need one. Here's the relevant setting:

py_modules = catalogue

commented

I am facing this error while importing spacy,

Traceback (most recent call last):
  File "/Users/arjun/environments/test3.6/lib/python3.6/site-packages/catalogue.py", line 8, in <module>
    import importlib.metadata as importlib_metadata
ModuleNotFoundError: No module named 'importlib.metadata'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/arjun/environments/test3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/arjun/environments/test3.6/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/Users/arjun/nlpparser/__init__.py", line 1, in <module>
    import spacy
  File "/Users/arjun/environments/test3.6/lib/python3.6/site-packages/spacy/__init__.py", line 10, in <module>
    from thinc.neural.util import prefer_gpu, require_gpu
  File "/Users/arjun/environments/test3.6/lib/python3.6/site-packages/thinc/__init__.py", line 8, in <module>
    from ._registry import registry
  File "/Users/arjun/environments/test3.6/lib/python3.6/site-packages/thinc/_registry.py", line 1, in <module>
    import catalogue
  File "/Users/arjun/environments/test3.6/lib/python3.6/site-packages/catalogue.py", line 10, in <module>
    import importlib_metadata
  File "/Users/arjun/environments/test3.6/lib/python3.6/site-packages/importlib_metadata/__init__.py", line 623, in <module>
    __version__ = version(__name__)
  File "/Users/arjun/environments/test3.6/lib/python3.6/site-packages/importlib_metadata/__init__.py", line 585, in version
    return distribution(distribution_name).version
  File "/Users/arjun/environments/test3.6/lib/python3.6/site-packages/importlib_metadata/__init__.py", line 285, in version
    return self.metadata['Version']
  File "/Users/arjun/environments/test3.6/lib/python3.6/site-packages/importlib_metadata/__init__.py", line 273, in metadata
    self.read_text('METADATA')
  File "/Users/arjun/environments/test3.6/lib/python3.6/site-packages/importlib_metadata/__init__.py", line 545, in read_text
    return self._path.joinpath(filename).read_text(encoding='utf-8')
AttributeError: 'PosixPath' object has no attribute 'read_text'

I am using Python 3.6, in CentOS Linux 7. Any idea @ines. TIA :)

commented

From the other issues I see that, pathlib is causing the problem. I am having wrapt_timeout_decorator as a dependency and which requires pathlib 🤕
Also I don't see this problem in MacOS, I have both of them installed and everything works good. But I face this in linux server. Gotta find a way! Will update if I could do some workaround