pganssle / zoneinfo

Reference implementation for the proposed standard library module zoneinfo
Other
52 stars 20 forks source link

ModuleNotFoundError: No module named 'zoneinfo' #122

Closed amotl closed 2 years ago

amotl commented 2 years ago

Dear Paul,

thanks a stack for maintaining this package. I just used it for the very first time, and copied the snippet from the README as advertised, and how I expected it to work out of the box. Using a try/except on an ImportError is usually what we are all used to do since ages, right?

Now I was baffled to find that in this case, Python 3.8 reports a ModuleNotFoundError. I think it probably always have been like that, ImportError would only be raised in from abc import foo-style cases, where the top-level package module exists, but not a submodule.

I just wanted to make sure you agree on this before I would submit a corresponding patch to the README.

try:
    import zoneinfo
except ModuleNotFoundError:
    from backports import zoneinfo

With kind regards, Andreas.

Reproduction

docker run --rm -it python:3.8-slim-bullseye
>>> import zoneinfo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'zoneinfo'
pganssle commented 2 years ago

Hi Andreas,

ModuleNotFoundError is a subclass of ImportError. You can catch either ImportError or ModuleNotFoundError, and I tend to just always catch ImportError. I'm not sure that the README needs to be updated.

amotl commented 2 years ago

Hi Paul,

oh you are right. I don't know why this issue happened to me. I will close it and may come back to it when I will be able to re-evaluate the problems I've observed, if any.

With kind regards, Andreas.

amotl commented 2 years ago

As expected, everything works flawlessly when using ImportError again with https://github.com/crate/crate-python/commit/4d655b6c2, tested on behalf of https://github.com/crate/crate-python/pull/466. I don't know what went wrong before, so again sorry for the noise.