Skip to content

fix: resolve relative import errors in emperor_dilemma, hex_ant, hex_…#427

Open
Vanya-kapoor wants to merge 2 commits intomesa:mainfrom
Vanya-kapoor:fixing-examples
Open

fix: resolve relative import errors in emperor_dilemma, hex_ant, hex_…#427
Vanya-kapoor wants to merge 2 commits intomesa:mainfrom
Vanya-kapoor:fixing-examples

Conversation

@Vanya-kapoor
Copy link

@Vanya-kapoor Vanya-kapoor commented Mar 21, 2026

Summary

Fixes ImportError: attempted relative import with no known parent package
in three examples that were broken on Mesa 3.3.1.

Examples fixed: emperor_dilemma, hex_ant, hex_snowflake
Change: from .module import Xfrom module import X (3 files, 1 line each)

Root cause

These examples use relative imports (from .agents import X) which require
the directory to be a Python package with a parent context. Running
solara run app.py from inside the example directory doesn't provide that
package context, so the import fails. Changing to absolute imports fixes
this without requiring __init__.py files or changes to how the examples
are run.

Verified

  • emperor_dilemma — import fix confirmed, model initializes correctly ✓
  • hex_snowflake — import fix confirmed, model initializes correctly ✓
  • hex_ant — import fix confirmed, but exposes a secondary bug:
    TypeError: 'PropertyLayer' object does not support item assignment
    This is a separate Mesa 3.x API change unrelated to the import fix.

GSoC contributor checklist

Context & motivation
I was auditing all the examples of mesa 3.0 and found hex_ant,hex_snowflake have ImportError: relative import with no known parent package. That's what I tried to fix

What I learned
I learned that the dot in from .model import X tells Python to look for the module relative to a parent package — but when you run solara run app.py directly from inside the example folder, Python has no parent package context, so it crashes. I also found that hex_ant had a second separate bug underneath the import error — a PropertyLayer API change
Learning repo
🔗 My learning repo: https://github.com/Vanya-kapoor/GSoC-learning-space/tree/main

Readiness checks

Copy link

@B2prakash B2prakash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on the relative import issue. The fix is clean — one line per file, minimal change, clear root cause explanation.
A few comments:

  1. The fix is correct — changing from .module import X to from module import X is the right approach for examples that are run standalone with solara run app.py. Since these aren't installed packages, relative imports won't work without parent package context.

  2. Nice work documenting the hex_ant secondary bug. Flagging the PropertyLayer item assignment error separately rather than trying to fix it in this PR is good practice — keeps the PR focused on one issue.

  3. GSoC checklist note: The "Another GSoC contributor has reviewed this PR" field is blank — I can fill that role. Consider adding my username there after this review.

  4. One question: Did you check if any other examples in the repo have the same relative import pattern? If so, might be worth listing them in the PR description so someone can fix those too (in a separate PR).

@Vanya-kapoor
Copy link
Author

thanks for reviewing my pr. In reply to this question-Did you check if any other examples in the repo have the same relative import pattern? Yes I did, only these 4 had the same issue.
This pr is ready to be merged. @jackiekazil @catherinedevlin @EwoutH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants