Skip to content
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 with importlib.metadata #110

Closed
sbraz opened this issue Apr 9, 2021 · 3 comments
Closed

Replace pkg_resources with importlib.metadata #110

sbraz opened this issue Apr 9, 2021 · 3 comments

Comments

@sbraz
Copy link
Owner

sbraz commented Apr 9, 2021

@miigotu As discussed in #50 (comment), do you have any good example of packages using importlib.metadata to expose a __version__ attribute?

I guess we'll have to use the backport for older Pythons too.

@sbraz
Copy link
Owner Author

sbraz commented Apr 9, 2021

It looks as straightforward as:

try:
    from importlib import metadata
except ImportError:
    import importlib_metadata as metadata

__version__ = metadata.version("pymediainfo")

@sbraz
Copy link
Owner Author

sbraz commented Apr 9, 2021

diff --git a/pymediainfo/__init__.py b/pymediainfo/__init__.py
index f05a5a5..efe1974 100644
--- a/pymediainfo/__init__.py
+++ b/pymediainfo/__init__.py
@@ -12,11 +12,14 @@ import warnings
 import xml.etree.ElementTree as ET
 from typing import Any, Dict, List, Optional, Tuple, Union
 
-from pkg_resources import DistributionNotFound, get_distribution
+try:
+    from importlib import metadata
+except ImportError:
+    import importlib_metadata as metadata  # type: ignore
 
 try:
-    __version__ = get_distribution("pymediainfo").version
-except DistributionNotFound:
+    __version__ = metadata.version("pymediainfo")
+except metadata.PackageNotFoundError:
     pass
 
 

That seems to work, now I just need to set the dependencies right in setup.py.

sbraz added a commit that referenced this issue Apr 9, 2021
This allows us to drop the requirement for setuptools.
sbraz added a commit that referenced this issue Apr 9, 2021
This allows us to drop the requirement for setuptools.
sbraz added a commit that referenced this issue Apr 10, 2021
This allows us to drop the requirement for setuptools.

Also make __version__ an empty string when the package isn't installed,
for improved consistency.

Co-Authored-By: miigotu <miigotu@gmail.com>
@sbraz
Copy link
Owner Author

sbraz commented Apr 10, 2021

@miigotu I'll push this latest commit with a version bump tomorrow. It's slightly late here and I want to take one last look when I'm properly awake. Thanks for the useful advice.

@sbraz sbraz closed this as completed in 8af1511 Apr 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant