Skip to content

Commit 9cbbcb5

Browse files
authored
importlib-related improvements (#66)
* Use importlib.import_module As recommended in docs: https://docs.python.org/3/library/importlib.html#importlib.__import__ * Use importlib.metadata rather than pkg_resources As recommended in option 5: https://packaging.python.org/guides/single-sourcing-package-version/ Closes #56 * Use importlib-metadata for own version * Update readme * Fix minor formatting errors using isort and black
1 parent 3a6797e commit 9cbbcb5

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ In line with [NEP 29][nep-29], this project supports:
128128

129129
_in progress_
130130

131-
132131
- Adopt [NEP 29][nep-29] and require Python version 3.7 or newer. ([#63](https://github.com/rasbt/watermark/pull/63), via contribution by [James Myatt](https://github.com/jamesmyatt))
133132
- Add Python 3.8 and 3.9 to Travis CI builds. ([#63](https://github.com/rasbt/watermark/pull/63), via contribution by [James Myatt](https://github.com/jamesmyatt))
133+
- Fix: Allow setup.py to run without install_requires already installed ([#67](https://github.com/rasbt/watermark/pull/67), via contribution by [James Myatt](https://github.com/jamesmyatt))
134134
- Major refactoring to improve code readability ([#64](https://github.com/rasbt/watermark/pull/64) and [65](https://github.com/rasbt/watermark/pull/65), via contribution by [Bahram Aghaei](https://github.com/GreatBahram))
135+
- Use importlib and importlib.metadata to determine package version numbers. ([#66](https://github.com/rasbt/watermark/pull/66), via contribution by [James Myatt](https://github.com/jamesmyatt))
135136

136137
#### v. 2.0.2 (November 19, 2019)
137138

setup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
name="watermark",
1515
license="newBSD",
1616
description=(
17-
"IPython magic function to print date/time stamps and"
17+
"IPython magic function to print date/time stamps and "
1818
"various system information."
1919
),
2020
author="Sebastian Raschka",
2121
author_email="[email protected]",
2222
url="https://github.com/rasbt/watermark",
2323
packages=find_packages(exclude=[]),
24-
install_requires=["ipython"],
24+
install_requires=[
25+
"ipython",
26+
'importlib-metadata < 3.0 ; python_version < "3.8"',
27+
],
2528
long_description=dedent(
2629
"""\
2730
An IPython magic extension for printing date and time stamps, version

watermark/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from __future__ import absolute_import
99

10-
from .watermark import *
1110
from .version import __version__
11+
from .watermark import *
1212

13-
__all__ = ['watermark']
13+
__all__ = ["watermark"]

watermark/version.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import pkg_resources
1+
try:
2+
import importlib.metadata as importlib_metadata
3+
except ImportError:
4+
# Running on pre-3.8 Python; use importlib-metadata package
5+
import importlib_metadata
26

37
try:
4-
__version__ = pkg_resources.get_distribution('watermark').version
8+
__version__ = importlib_metadata.version("watermark")
59
except Exception:
6-
__version__ = 'unknown'
10+
__version__ = "unknown"

watermark/watermark.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,36 @@
66
License: BSD 3 clause
77
"""
88

9-
109
from __future__ import absolute_import
10+
1111
import datetime
12+
import importlib
1213
import platform
1314
import subprocess
1415
import time
1516
import types
1617
from multiprocessing import cpu_count
1718
from socket import gethostname
1819

20+
try:
21+
import importlib.metadata as importlib_metadata
22+
except ImportError:
23+
# Running on pre-3.8 Python; use importlib-metadata package
24+
import importlib_metadata
25+
1926
import IPython
20-
import pkg_resources
2127
from IPython.core.magic import Magics, line_magic, magics_class
22-
from IPython.core.magic_arguments import argument, \
23-
magic_arguments, parse_argstring
24-
from pkg_resources import DistributionNotFound
28+
from IPython.core.magic_arguments import (argument, magic_arguments,
29+
parse_argstring)
2530

2631
from .version import __version__
2732

2833

29-
class PackageNotFoundError(Exception):
30-
pass
31-
32-
3334
@magics_class
3435
class WaterMark(Magics):
3536
"""
3637
IPython magic function to print date/time stamps
3738
and various system information.
38-
3939
"""
4040

4141
@magic_arguments()
@@ -162,13 +162,13 @@ def _get_package_version(self, pkg_name):
162162
if pkg_name == "scikit-learn":
163163
pkg_name = "sklearn"
164164
try:
165-
imported = __import__(pkg_name)
165+
imported = importlib.import_module(pkg_name)
166166
except ImportError:
167167
version = "not installed"
168168
else:
169169
try:
170-
version = pkg_resources.get_distribution(pkg_name).version
171-
except DistributionNotFound:
170+
version = importlib_metadata.version(pkg_name)
171+
except importlib_metadata.PackageNotFoundError:
172172
try:
173173
version = imported.__version__
174174
except AttributeError:

0 commit comments

Comments
 (0)