Let's start with a concrete example.
It often happens that I want to use from importlib.resources import files, but that only exists in 3.9+.
In my code, I end up doing:
try:
from importlib.resources import files # ... and any other things you want to get
except (ImportError, ModuleNotFoundError):
from importlib_resources import files # pip install importlib_resources
But I feel shame when copy/pasting this code everywhere. Additionally, it still doesn't solve my problem "completely".
The question is: Do I include importlib_resources, a third-party backport of importlib.resources, in my setup.cfg install_requires? If I don't, my code will fail on python < 3.9. If I do include it, I'm overkilling my dependencies.
Ideally, I'd like a solution where I can just do
from importlib_resources import files
in my code, and trust that the setup.cfg took care of doing two things:
- telling the install to only do a
pip install importlib-resources if python version < 3.9
- telling it that
importlib_resources is an alias for importlib.resources if in 3.9+, and of importlib_resources (the pip install backport) if not.
Trying to solve the first problem only, I did this.
install_requires =
dol
importlib_resources; python_version < '3.9'
But it doesn't condition the install at all (it's installed on both 3.8 and 3.10!).
Not sure how to even start with the second "aliasing" desire.
Let's start with a concrete example.
It often happens that I want to use
from importlib.resources import files, but that only exists in 3.9+.In my code, I end up doing:
But I feel shame when copy/pasting this code everywhere. Additionally, it still doesn't solve my problem "completely".
The question is: Do I include
importlib_resources, a third-party backport ofimportlib.resources, in mysetup.cfginstall_requires? If I don't, my code will fail on python < 3.9. If I do include it, I'm overkilling my dependencies.Ideally, I'd like a solution where I can just do
in my code, and trust that the
setup.cfgtook care of doing two things:pip install importlib-resourcesif python version < 3.9importlib_resourcesis an alias forimportlib.resourcesif in 3.9+, and ofimportlib_resources(the pip install backport) if not.Trying to solve the first problem only, I did this.
But it doesn't condition the install at all (it's installed on both 3.8 and 3.10!).
Not sure how to even start with the second "aliasing" desire.