I want to keep __version__
of my package in a single source file that would be accessible to __init__
and also to be able to import it in setup
without being dependent on the package's dependencies.
Assume I have a package with a single module module_a
with version 1.0.0
and have a single dependency - module_b
.
module-a/src/module_a/__init__.py
:
__version__ = `1.0.0`
from module_a.inner.foo import bar
module-a/src/module_a/inner/foo.py
:
from module_b import baz
...
def bar():
baz.do_something()
module-a/setup.py
:
from setuptools import setup
from module_a import __version__
setup(
version=__version__,
...,
)
Now when I want to update module_a
to version 1.0.1
I only need to update __init__.py
and not setup.py
.
The problem is now that setup
depends on module-b
being installed, which is not good.
My current workaround:
module-a/src/module_a.__init__.py
:
__version__ = `1.0.0`
try:
from module_a.foo import bar
except ImportError:
# this should happen only when import __version__, __name__ from setup.py
pass
Now setup does not depend on module-b
:)
But the problem is that if for some reason
from module_a.inner.foo import bar
fails (for instance in case - module-b
was not installed) the import will fail silently and not raise a proper exception and module_a.foo
won't be accessible from module_a
.
Is there a proper way to fix this ?