Skip to content

Relative import error when running model-checker on submodule examples.py files #76

@benbrastmckie

Description

@benbrastmckie

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

  1. Run model-checker to create a new project (this works correctly)
  2. Navigate to a subtheory directory: cd subtheories/counterfactual/
  3. 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

  1. 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.

  2. Support absolute imports: Allow the examples.py files to use absolute imports when run directly (perhaps with a fallback mechanism).

  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions