mesa-examples icon indicating copy to clipboard operation
mesa-examples copied to clipboard

Fix emperor_dilemma: relative imports and outdated SpaceRenderer API

Open mehaksalil opened this issue 9 months ago • 10 comments

The emperor_dilemma example is currently broken and cannot be run due to two issues:

  1. Relative imports in app.py, model.py, and agents.py are incompatible with how Solara loads example modules.

  2. SpaceRenderer.setup_agents() no longer exists in the current Mesa API.

Both issues prevent the example from running at all.

mehaksalil avatar Feb 25 '26 20:02 mehaksalil

  1. Relative imports in app.py, model.py, and agents.py are incompatible with how Solara loads example modules.

As far as I know, it is working fine.

  1. SpaceRenderer.setup_agents() no longer exists in the current Mesa API.

It exists in the current API.

Both issues prevent the example from running at all.

Any MRE to support your argument?

codebreaker32 avatar Mar 01 '26 06:03 codebreaker32

Here's a screenshot of the error on a fresh clone following the README instructions exactly. The relative import fails when running solara run app.py from inside the emperor_dilemma directory as the README instructs. This is reproducible and not environment-specific. I understand the tests are failing with my fix, I'm working on a solution that satisfies both Solara's module loading and the CI test runner. Happy to collaborate on the right approach if you have a preferred solution in mind.

Image

mehaksalil avatar Mar 01 '26 08:03 mehaksalil

When i fix those imports, (by using absolute imports) this was the next error i found:

Image

I ran the example, according to the README provided. Please let me know if I'm not supposed to be facing this error and right way to run the example

mehaksalil avatar Mar 01 '26 08:03 mehaksalil

Please share complete trace stack

codebreaker32 avatar Mar 01 '26 08:03 codebreaker32

ERROR: Traceback (most recent call last): File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\starlette\routing.py", line 694, in lifespan async with self.lifespan_context(app) as maybe_state: File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\starlette\routing.py", line 571, in aenter await self._router.startup() File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\starlette\routing.py", line 673, in startup handler() File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solara\server\starlette.py", line 620, in on_startup appmod.ensure_apps_initialized() File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solara\util.py", line 345, in wrapper return_value = f() ^^^ File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solara\server\app.py", line 542, in ensure_apps_initialized app.init() File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solara\server\app.py", line 106, in init app = self._execute() ^^^^^^^^^^^^^^^ File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solara\server\app.py", line 153, in _execute routes = [solara.autorouting._generate_route_path(self.path, first=True, initial_namespace=initial_namespace)] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solara\autorouting.py", line 525, in _generate_route_path module = source_to_module(subpath, initial_namespace=initial_namespace) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solara\autorouting.py", line 39, in source_to_module exec(ast, mod.dict) File "C:\Users\mehak\mesa\mesa-examples\examples\emperor_dilemma\app.py", line 10, in
from .model import EmperorModel ImportError: attempted relative import with no known parent package

ERROR: Application startup failed. Exiting.

mehaksalil avatar Mar 01 '26 08:03 mehaksalil

second error faced:

ERROR: Traceback (most recent call last): File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\starlette\routing.py", line 694, in lifespan async with self.lifespan_context(app) as maybe_state: File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\starlette\routing.py", line 571, in aenter await self._router.startup() File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\starlette\routing.py", line 673, in startup handler() File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solara\server\starlette.py", line 620, in on_startup appmod.ensure_apps_initialized() File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solara\util.py", line 345, in wrapper return_value = f() ^^^ File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solara\server\app.py", line 542, in ensure_apps_initialized app.init() File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solara\server\app.py", line 106, in init app = self._execute() ^^^^^^^^^^^^^^^ File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solara\server\app.py", line 153, in _execute routes = [solara.autorouting._generate_route_path(self.path, first=True, initial_namespace=initial_namespace)] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\mehak\AppData\Local\Programs\Python\Python311\Lib\site-packages\solae-packages\solara\autorouting.py", line 39, in source_to_module exec(ast, mod.dict) File "C:\Users\mehak\mesa\mesa-examples\examples\emperor_dilemma\app.py", line 91, in ).setup_agents(emperor_portrayal) ^^^^^^^^^^^^ AttributeError: 'SpaceRenderer' object has no attribute 'setup_agents'

ERROR: Application startup failed. Exiting.

mehaksalil avatar Mar 01 '26 08:03 mehaksalil

I checked it locally and relative import issue is genuine So I'd suggest moving agents.py and model.py to a separate subpackage in emperor_dilemma directory similar to how other examples are doing

I still couldn't reproduce the second issue

codebreaker32 avatar Mar 01 '26 09:03 codebreaker32

For the second issue, SpaceRenderer.setup_agents() doesn't exist in the current Mesa release. This may be a version difference. Could you share which Mesa version you're running? On the latest release the correct pattern is .render(agent_portrayal=...) as used in examples like rumormill.

mehaksalil avatar Mar 01 '26 09:03 mehaksalil

Could you share which Mesa version you're running? On the latest release the correct pattern is .render(agent_portrayal=...) as used in examples like rumormill.

You are mistaken. See

https://github.com/mesa/mesa/blob/2cf3a6334988ab9d364e98d3ffd4108d8c63176d/mesa/visualization/space_renderer.py#L164

https://github.com/mesa/mesa/blob/2cf3a6334988ab9d364e98d3ffd4108d8c63176d/mesa/visualization/space_renderer.py#L359

codebreaker32 avatar Mar 01 '26 09:03 codebreaker32

That makes sense, I was on Python 3.11 with Mesa 3.3.1 which doesn't have setup_agents. I'll upgrade to Python 3.12 and retest. I'll drop the SpaceRenderer fix from the PR and focus only on the relative import restructure as you suggested.

mehaksalil avatar Mar 01 '26 10:03 mehaksalil