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

Avoid importing pkg_resources #2571

Closed
bluetech opened this issue Aug 27, 2020 · 0 comments · Fixed by #2567
Closed

Avoid importing pkg_resources #2571

bluetech opened this issue Aug 27, 2020 · 0 comments · Fixed by #2567
Assignees
Labels
performance go faster! use less memory!

Comments

@bluetech
Copy link

Hypothesis recently started importing pkg_resources which is notoriously slow to import. A better alternative these days is to use importlib.metadata, added in Python 3.8, or its backport importlib_metadata. Doing so would look something like this (completely untested, just to get the idea across):

diff --git a/hypothesis-python/src/hypothesis/entry_points.py b/hypothesis-python/src/hypothesis/entry_points.py
index 8cc4137ba..a5d670143 100644
--- a/hypothesis-python/src/hypothesis/entry_points.py
+++ b/hypothesis-python/src/hypothesis/entry_points.py
@@ -20,9 +20,18 @@ custom types, running the relevant code when *hypothesis* is imported instead of
 your package.
 """
 
-import pkg_resources
+try:
+    if sys.version_info >= (3, 8):
+        from importlib import metadata as importlib_metadata
+    else:
+        import importlib_metadata
+except ImportError:
+    import pkg_resources
 
-
-def run():
-    for entry_point in pkg_resources.iter_entry_points("hypothesis"):
-        entry_point.load()  # pragma: no cover
+    def run():
+        for entry_point in pkg_resources.iter_entry_points("hypothesis"):
+            entry_point.load()  # pragma: no cover
+else:
+    def run():
+        for entry_point in importlib_metadata.entry_points().get("hypothesis", []):
+            entry_point.load()  # pragma: no cover
@Zac-HD Zac-HD added the performance go faster! use less memory! label Aug 28, 2020
@Zac-HD Zac-HD self-assigned this Aug 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance go faster! use less memory!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants