Skip to content

Commit 1e10b03

Browse files
Replace pkg_resources with importlib.metadata (#1674)
Use `importlib.metadata` from the standard library (added in Python 3.8) to retrieve the package version at runtime. A bit faster than using the `pkg_resources` which adds some overhead and sets a runtime dependency on `setuptools`. * rename version variables in __init__.py Co-authored-by: TIAN Dongdong <[email protected]>
1 parent 90df7b2 commit 1e10b03

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

pygmt/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
"""
2020

2121
import atexit as _atexit
22-
23-
from pkg_resources import get_distribution
22+
from importlib.metadata import version
2423

2524
# Import modules to make the high-level GMT Python API
2625
from pygmt import datasets
@@ -63,7 +62,7 @@
6362
)
6463

6564
# Get semantic version through setuptools-scm
66-
__version__ = f'v{get_distribution("pygmt").version}' # e.g. v0.1.2.dev3+g0ab3cd78
65+
__version__ = f'v{version("pygmt")}' # e.g. v0.1.2.dev3+g0ab3cd78
6766
__commit__ = __version__.split("+g")[-1] if "+g" in __version__ else "" # 0ab3cd78
6867

6968
# Start our global modern mode session
@@ -135,10 +134,9 @@ def _get_ghostscript_version():
135134

136135
for gs_cmd in cmds:
137136
try:
138-
version = subprocess.check_output(
137+
return subprocess.check_output(
139138
[gs_cmd, "--version"], universal_newlines=True
140139
).strip()
141-
return version
142140
except FileNotFoundError:
143141
continue
144142
return None
@@ -148,10 +146,9 @@ def _get_gmt_version():
148146
Get GMT version.
149147
"""
150148
try:
151-
version = subprocess.check_output(
149+
return subprocess.check_output(
152150
["gmt", "--version"], universal_newlines=True
153151
).strip()
154-
return version
155152
except FileNotFoundError:
156153
return None
157154

0 commit comments

Comments
 (0)