Don't use pkg_resources.
authorwhitequark <whitequark@whitequark.org>
Tue, 30 Jun 2020 22:08:59 +0000 (22:08 +0000)
committerwhitequark <whitequark@whitequark.org>
Tue, 30 Jun 2020 22:11:47 +0000 (22:11 +0000)
This package is deprecated and introduces a massive amount of startup
latency. On my machine with 264 installed Python packages, it reduces
the time required to `import nmigen` from ~100ms to ~200ms.

nmigen/__init__.py
setup.py

index 5646c6de35c14fad7480b09cb49bfdbc3e25f14a..78a8dec0c3b15ad49cef3dc925a2ef0c128804d6 100644 (file)
@@ -1,8 +1,8 @@
-import pkg_resources
 try:
-    __version__ = pkg_resources.get_distribution(__name__).version
-except pkg_resources.DistributionNotFound:
-    pass
+    from importlib import metadata as importlib_metadata # py3.8+ stdlib
+except ImportError:
+    import importlib_metadata # py3.7- shim
+__version__ = importlib_metadata.version(__package__)
 
 
 from .hdl import *
index fe6bd49512a9dfdf7603706ce8ef7043d9d49f7f..4d436d8cdc9026572d435e93109761dc0a824f76 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -25,7 +25,7 @@ setup(
     python_requires="~=3.6",
     setup_requires=["setuptools", "setuptools_scm"],
     install_requires=[
-        "importlib_metadata; python_version<'3.8'",  # for nmigen._yosys
+        "importlib_metadata; python_version<'3.8'",  # for __version__ and nmigen._yosys
         "importlib_resources; python_version<'3.9'", # for nmigen._yosys
         "pyvcd~=0.2.0", # for nmigen.pysim
         "Jinja2~=2.11", # for nmigen.build