fix: resolve relative import errors in emperor_dilemma, hex_ant, hex_…#427
fix: resolve relative import errors in emperor_dilemma, hex_ant, hex_…#427Vanya-kapoor wants to merge 2 commits intomesa:mainfrom
Conversation
for more information, see https://pre-commit.ci
B2prakash
left a comment
There was a problem hiding this comment.
Good catch on the relative import issue. The fix is clean — one line per file, minimal change, clear root cause explanation.
A few comments:
-
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.
-
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.
-
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.
-
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).
|
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. |
Summary
Fixes
ImportError: attempted relative import with no known parent packagein three examples that were broken on Mesa 3.3.1.
Examples fixed: emperor_dilemma, hex_ant, hex_snowflake
Change:
from .module import X→from module import X(3 files, 1 line each)Root cause
These examples use relative imports (
from .agents import X) which requirethe directory to be a Python package with a parent context. Running
solara run app.pyfrom inside the example directory doesn't provide thatpackage context, so the import fails. Changing to absolute imports fixes
this without requiring
__init__.pyfiles or changes to how the examplesare 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 assignmentThis 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
pytestandruffchecks not applicable@jackiekazil @EwoutH