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 #137

Merged
merged 1 commit into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pynndescent/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import pkg_resources
import sys

import numba

from .pynndescent_ import NNDescent, PyNNDescentTransformer

if sys.version_info[:2] >= (3, 8):
import importlib.metadata as importlib_metadata
else:
import importlib_metadata

Comment on lines +7 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option here is to just catch the ImportError (and you wouldn't need to import sys)

Suggested change
if sys.version_info[:2] >= (3, 8):
import importlib.metadata as importlib_metadata
else:
import importlib_metadata
try:
import importlib.metadata as importlib_metadata
except ImportError:
import importlib_metadata

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to swallow any exceptions

Copy link
Collaborator

@jamestwebber jamestwebber Feb 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough! Suggested because it's a fairly common pattern (there's an except ImportError in this file already).

A totally different (over-the-top) solution to this issue would be to rewrite the packaging to use setup.cfg and pyproject.toml files, at which point setup.py is no longer necessary and the version can be stored as a literal in this file (setup.cfg can read it). I only learned how to do this yesterday, so I'm mad with power. 😂

edit: although this is a relatively new feature of setuptools and might not work well in all environments, so maybe it's better to wait on that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'm all for switching everything to PEP 621 and reading metadata through importlib.metadata, but for now I guess __version__ and importlib_metadata will have to do ¯\_(ツ)_/¯

# Workaround: https://github.com/numba/numba/issues/3341
if numba.config.THREADING_LAYER == "omp":
try:
Expand All @@ -12,4 +19,4 @@
# might be a missing symbol due to e.g. tbb libraries missing
numba.config.THREADING_LAYER = "workqueue"

__version__ = pkg_resources.get_distribution("pynndescent").version
__version__ = importlib_metadata.version("pynndescent")
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def readme():
"numba >= 0.51.2",
"llvmlite >= 0.30",
"joblib >= 0.11",
'importlib-metadata >= 4.8.1; python_version < "3.8"',
],
"ext_modules": [],
"cmdclass": {},
Expand Down