I have tried uninstalling the mesa package and reinstalling it, I have tried both upper and lowercase g for Grid on line 3, But the error continues. the code is below:

import mesa
import matplotlib.pyplot as plt
from mesa.space import grid

class MoneyAgent(mesa.Agent):
*
*
*
        # Create a grid of agents based on wealth
        grid_width = int(round(self.num_agents**0.5))
        grid_height = int(round(self.num_agents**0.5))
        grid = [[[] for j in range(grid_width)] for i in range(grid_height)]
        for i, a in enumerate(self.schedule.agents):
            x = i % grid_width
            y = i // grid_width
            grid[y][x] = a

        # Display the grid
        fig, ax = plt.subplots(figsize=(10, 10))
        ax.set_xticks([])
        ax.set_yticks([])
        for i in range(grid_height):
            for j in range(grid_width):
                if grid[i][j]:
                    size = grid[i][j].wealth * 5
                    color = 'green' if grid[i][j].wealth >= 1 else 'red'
                    ax.add_artist(plt.Circle((j + 0.5, grid_height - i - 0.5), size, color=color))
                    ax.text(j + 0.5, grid_height - i - 0.5, str(grid[i][j].wealth), ha='center', va='center')
        plt.show()

I get the following error code:

(base) D:\>python
Python 3.9.13 (main, Aug 25 2022, 23:51:50) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from money_model_cht import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\money_model_cht.py", line 3, in <module>
    from mesa.space import grid
ImportError: cannot import name 'grid' from 'mesa.space' (C:****\anaconda3\lib\site-packages\mesa\space.py)
1

There are 1 best solutions below

0
amanbirs On

You have to specify the type of Grid. Options are SingleGrid, MultiGrid, HexGrid, ContinuousSpace or NetworkGrid

SingleGrid will work if you want only one agent in a cell. MultiGrid can support more than one agent per cell

https://mesa.readthedocs.io/en/stable/apis/space.html