on-internet
← Community

Is there a way to use lazy imports in backwards-compatible code?

Stack Overflow ·

I'm writing some code that I want to make use of lazy imports, which were introduced in Python 3.15. However, I want the code to run fine on Python < 3.15 too, albeit without lazy imports. Just running the following code would raise a SyntaxError on previous Python versions: lazy import numpy as np I tried using a conditional: import sys if sys.version_info >= (3, 15): lazy import numpy as np else: import numpy as np But it still raises a syntax error on Python 3.10. And it would be cumbersome anyway to have to write that in every file.

Comments

Sign in or sign up to comment and upvote.

No comments yet.