From aa60dc88c26bbcb5d16dd70edca92ee40f05558c Mon Sep 17 00:00:00 2001 From: Floris Lambrechts Date: Thu, 26 Jul 2018 14:46:41 +0200 Subject: [PATCH 1/2] Make __version__ a module-level attribute By moving __version__ to __init__.py, it becomes an attribute of the module. This is recommended in PEP 396 and a common practice in many contemporary packages. This enables the following: >>> import mypackage >>> print(mypackage.__version__) '5.2.0' --- mypackage/__init__.py | 4 ++++ mypackage/__version__.py | 2 -- setup.py | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/mypackage/__init__.py b/mypackage/__init__.py index bb67a43..e7ebf36 100644 --- a/mypackage/__init__.py +++ b/mypackage/__init__.py @@ -1 +1,5 @@ + +from .__version__ import VERSION +__version__ = '.'.join(map(str, VERSION)) + from .core import * diff --git a/mypackage/__version__.py b/mypackage/__version__.py index 07a0333..7dffbd3 100644 --- a/mypackage/__version__.py +++ b/mypackage/__version__.py @@ -4,5 +4,3 @@ # 88 YY 88 dP 88 dP""""Yb YboodP 88 Yb dP""""Yb YboodP 888888 VERSION = (5, 2, 0) - -__version__ = '.'.join(map(str, VERSION)) diff --git a/setup.py b/setup.py index 6825e57..702b973 100644 --- a/setup.py +++ b/setup.py @@ -50,6 +50,7 @@ about = {} if not VERSION: with open(os.path.join(here, NAME, '__version__.py')) as f: exec(f.read(), about) + about['__version__'] = '.'.join(map(str, about['VERSION'])) else: about['__version__'] = VERSION From cb6ec31e3305aa70c6daaf5c47c46fda5f59e76d Mon Sep 17 00:00:00 2001 From: Floris Lambrechts Date: Sun, 16 Dec 2018 21:39:00 +0100 Subject: [PATCH 2/2] Rename __version__.py to _version.py This avoids a name collision with mypackage.__version__ (the string). In addition, the single underscore shows it's a 'private' module that has no special meaning to the Python interpreter. --- mypackage/__init__.py | 2 +- mypackage/{__version__.py => _version.py} | 0 setup.py | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) rename mypackage/{__version__.py => _version.py} (100%) diff --git a/mypackage/__init__.py b/mypackage/__init__.py index e7ebf36..a21f065 100644 --- a/mypackage/__init__.py +++ b/mypackage/__init__.py @@ -1,5 +1,5 @@ -from .__version__ import VERSION +from ._version import VERSION __version__ = '.'.join(map(str, VERSION)) from .core import * diff --git a/mypackage/__version__.py b/mypackage/_version.py similarity index 100% rename from mypackage/__version__.py rename to mypackage/_version.py diff --git a/setup.py b/setup.py index 702b973..4b2ffb8 100644 --- a/setup.py +++ b/setup.py @@ -45,10 +45,10 @@ try: except FileNotFoundError: long_description = DESCRIPTION -# Load the package's __version__.py module as a dictionary. +# Load the package's _version.py module as a dictionary. about = {} if not VERSION: - with open(os.path.join(here, NAME, '__version__.py')) as f: + with open(os.path.join(here, NAME, '_version.py')) as f: exec(f.read(), about) about['__version__'] = '.'.join(map(str, about['VERSION'])) else: