Bug Description
When running model-checker examples.py from within a subdirectory (e.g., counterfactual/), the tool fails with a relative import error.
Steps to Reproduce
- Run
model-checker to create a new project (this works correctly)
- Navigate to a subtheory directory:
cd subtheories/counterfactual/
- Run:
model-checker examples.py
Expected Behavior
The model-checker should be able to load and run the examples.py file from the subdirectory.
Actual Behavior
The following error occurs:
Traceback (most recent call last):
File "/Users/nicky/Library/Python/3.13/lib/python/site-packages/model_checker/builder/strategies.py", line 179, in import_module
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 1026, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "/Users/nicky/Documents/project_ten/subtheories/counterfactual/examples.py", line 34, in <module>
from ...operators import LogosOperatorRegistry
ImportError: attempted relative import with no known parent package
During handling of the above exception, another exception occurred:
model_checker.builder.errors.PackageImportError: Failed to import module 'examples'
Context: Path: examples.py, Error: attempted relative import with no known parent package
Suggestion: Check module syntax and dependencies.
Root Cause Analysis
The issue appears to be that when model-checker directly loads a Python file, it doesn't establish the proper package context for relative imports to work. The file subtheories/counterfactual/examples.py contains:
from ...operators import LogosOperatorRegistry
from ...semantic import LogosSemantics, LogosProposition, LogosModelStructure
These relative imports expect the file to be loaded as part of a package structure, but when model-checker loads it directly, Python doesn't know what the parent packages are.
Potential Solutions
-
Modify model-checker's import strategy: When loading a module, detect if it's part of a package structure and set up the proper package context.
-
Support absolute imports: Allow the examples.py files to use absolute imports when run directly (perhaps with a fallback mechanism).
-
Add a package discovery mechanism: Automatically detect and set up the package hierarchy based on the presence of __init__.py files.
Environment
- Python: 3.13
- model-checker version: (latest from pip)
- OS: macOS Darwin 24.6.0
Workaround
Currently, users may need to run the examples from the project root directory or modify the import statements to be absolute imports when running files directly.
Bug Description
When running
model-checker examples.pyfrom within a subdirectory (e.g.,counterfactual/), the tool fails with a relative import error.Steps to Reproduce
model-checkerto create a new project (this works correctly)cd subtheories/counterfactual/model-checker examples.pyExpected Behavior
The model-checker should be able to load and run the examples.py file from the subdirectory.
Actual Behavior
The following error occurs:
Root Cause Analysis
The issue appears to be that when
model-checkerdirectly loads a Python file, it doesn't establish the proper package context for relative imports to work. The filesubtheories/counterfactual/examples.pycontains:These relative imports expect the file to be loaded as part of a package structure, but when model-checker loads it directly, Python doesn't know what the parent packages are.
Potential Solutions
Modify model-checker's import strategy: When loading a module, detect if it's part of a package structure and set up the proper package context.
Support absolute imports: Allow the examples.py files to use absolute imports when run directly (perhaps with a fallback mechanism).
Add a package discovery mechanism: Automatically detect and set up the package hierarchy based on the presence of
__init__.pyfiles.Environment
Workaround
Currently, users may need to run the examples from the project root directory or modify the import statements to be absolute imports when running files directly.