I have previously done the following to fetch a version string:
>>> filepath = './somemodule/__init__.py'
>>> name = 'dummy'
>>> module_source = imp.load_source(name, filepath)
>>> module_source.__version__
1.0.0
Now, when imp
has been deprecated in Python 3 (I'm on 3.7.1), what would be a nice replacement for load_source
which will just require the std library?
This seems a little convoluted to me AND load_module
is actually deprecated:
>>> from importlib.machinery import SourceFileLoader
>>> loaded = SourceFileLoader(name, filepath).load_module()
>>> loaded.__version__
1.0.0
EDIT #1:
This is discussed in Import arbitrary python source file. (Python 3.3+) and one of the solutions which came up there is:
>>> loader = importlib.machinery.SourceFileLoader(name, filepath)
>>> mod = types.ModuleType(loader.name)
>>> loader.exec_module(mod)
>>> mod.__version__
1.0.0