-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace pkg_resources usage with packaging + importlib.metadata #392
Replace pkg_resources usage with packaging + importlib.metadata #392
Conversation
Hi @jacob-indigo For the implementation details, I think we can group everything into one try:
import importlib.metadata
__version__ = importlib.metadata.version('GeoAlchemy2')
except ImportError:
try:
from pkg_resources import DistributionNotFound
from pkg_resources import get_distribution
__version__ = get_distribution('GeoAlchemy2').version
except: # pragma: no cover
pass WDYT? |
@adrien-berchet That works for me! Just pushed that change. |
After some more thinking, I think we have to write this if we want to handle all cases without a bare Exception: try:
import importlib.metadata
except ImportError:
try:
from pkg_resources import DistributionNotFound
from pkg_resources import get_distribution
except ImportError: # pragma: no cover
pass
else:
try:
__version__ = get_distribution('GeoAlchemy2').version
except DistributionNotFound: # pragma: no cover
pass
else:
try:
__version__ = importlib.metadata.version('GeoAlchemy2')
except importlib.metadata.PackageNotFoundError: # pragma: no cover
pass Do you agree? Did I miss anything again? |
LGTM now. |
040777e
to
c7351b2
Compare
@adrien-berchet Good catch on the missing |
Thanks! |
geoalchemy2
is currently fairly slow to import (with and without__pycache__
being built) and, more importantly, it seems gets substantially slower as the Python environment gets larger. This is not an issue withgeoalchemy2
itself though.SQLAlchemy
is the main culprit for the slow baseline import time andpkg_resources
is the culprit for the environment size-dependent import time (see: pypa/setuptools#926).The PR does two things:
pkg_resources
inshape.py
withpackaging
(an existing dependency)importlib.metadata
instead ofpkg_resources
in__init__.py
, falling back topkg_resources
if you're using Python <3.8Profiling import time before and after these changes:
The profiling was done in 4 different
virtualenv
s, covering a range of environment sizes (arbitrarily named here). Tiny is just what's inrequirement-rtd.txt
plusshapely
: requirements.tiny.txt, and the other environments are real example requirements files from internal projects (not included here but available upon request).These times were produced simply using:
Additionally, here are some visualizations of the
python -X importtime
outputs for the same profiling command in the X-Large environment:Before:
After: