Skip to content

Commit

Permalink
Replace pkg_resources with importlib.metadata (GenericMappingTools#1674)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
2 people authored and Josh Sixsmith committed Dec 21, 2022
1 parent 63f41da commit e6a19b9
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions pygmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"""

import atexit as _atexit

from pkg_resources import get_distribution
from importlib.metadata import version

# Import modules to make the high-level GMT Python API
from pygmt import datasets
Expand Down Expand Up @@ -63,7 +62,7 @@
)

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

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

for gs_cmd in cmds:
try:
version = subprocess.check_output(
return subprocess.check_output(
[gs_cmd, "--version"], universal_newlines=True
).strip()
return version
except FileNotFoundError:
continue
return None
Expand All @@ -148,10 +146,9 @@ def _get_gmt_version():
Get GMT version.
"""
try:
version = subprocess.check_output(
return subprocess.check_output(
["gmt", "--version"], universal_newlines=True
).strip()
return version
except FileNotFoundError:
return None

Expand Down

0 comments on commit e6a19b9

Please sign in to comment.