Fix emperor_dilemma: relative imports and outdated SpaceRenderer API
The emperor_dilemma example is currently broken and cannot be run due to two issues:
-
Relative imports in app.py, model.py, and agents.py are incompatible with how Solara loads example modules.
-
SpaceRenderer.setup_agents() no longer exists in the current Mesa API.
Both issues prevent the example from running at all.
- 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.
- 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?
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.
When i fix those imports, (by using absolute imports) this was the next error i found:
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
Please share complete trace stack
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.
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
ERROR: Application startup failed. Exiting.
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
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.
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
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.