I have a Python package with the following structure:
├── setup.cfg
├── setup.py
└── src
└── myproject
├── base.py
├── __init__.py
└── version.py
In setup.cfg, the version is defined with attr:
[metadata]
name = myproject
version = attr: myproject.version.__version__
With version.py being simply:
__version__ = "1.2.3"
The package have dependencies listed in setup.cfg. On the other hand, the package's top level __init__.py imports some objects of the package:
from myproject.base import Stuff
__all__ = ["Stuff"]
And, obviously, base.py imports some objects from the dependencies.
The problem is now that if I try to use python setup.py when the dependencies are not installed, I have a ModuleNotFoundError/ImportError, since __init__.py imports objects importing these dependencies (this is very to case 6 in the Python documentation on managing version file):
python setup.py --version
Traceback (most recent call last):
<snip>
ModuleNotFoundError: No module named 'dependency_module'
which makes installation of the dependencies impossible. So what should be the best strategy to target the version file?
So far, I have tried this weird exception handling approach on __init__.py:
try:
from myproject.base import Stuff
except ImportError:
pass
Is there a better way? Or another approach?