mesa icon indicating copy to clipboard operation
mesa copied to clipboard

networkx is not in the dependencies

Open Khalil-Youssefi opened this issue 8 months ago • 4 comments

Describe the bug networkx is not in the dependencies!

Expected behavior networkx must be added to mesa's python dependencies so that pip install it.

To Reproduce just do a pip insall mesa and try to run a model with mesa.discrete_space. Then it won't work.

Additional context I don't think it matters, but python version was 3.14.

Khalil-Youssefi avatar Mar 22 '26 11:03 Khalil-Youssefi

Hey @Khalil-Youssefi Good catch on the error. However, looking at the pyproject.toml, the core team intentionally moved networkx to the optional-dependencies list to keep the core package lightweight. Users are currently supposed to install it via pip install mesa[network].

To the Maintainers: It looks like mesa.discrete_space might be doing a top-level import networkx, which breaks the module for anyone using the base installation. Would you prefer a PR that:

- Wraps the import in a` try/except ImportError` with a friendly warning to `pip install mesa[network]` ?
- Delays the **networkx** import by moving it inside the specific **NetworkGrid** class/methods?

Let me know your preferred architecture, and I’ll gladly push the fix!

laxmi2577 avatar Mar 22 '26 12:03 laxmi2577

I had a quick look and think there's a broader issue here, related to #2343.

What is the broader issue? We implicitly assume that people use pip install mesa[rec]. This does include networkx. However, if people just do a simple pip install mesa any import mesa or from mesa.x import` will fail, because, as @laxmi2577 points out, discrete_space imports network, and network imports networkx.

There are at least two options.

  • in the toml, move networkx into the dependencies list, and remove the separate network entry
  • in network.py, import networkx inside Network.__init__

Which way we go depends on how we envisage people using Mesa. I am fine with either option, but would be curious to get the perspective of other maintainers.

quaquel avatar Mar 22 '26 13:03 quaquel

Thanks @quaquel for the context! Linking this to the broader eager-import discussion in #2343 makes a lot of sense.

Between the two options, moving the networkx import inside Network.init (and any other relevant class methods) seems like the most robust approach. It preserves the original goal of keeping the core mesa installation lightweight, while fully resolving the crash for base users who just want to use standard grids.

I can spin up a targeted PR for this specific lazy-import fix so the team has a concrete implementation to review. I'll get that pushed shortly!

laxmi2577 avatar Mar 22 '26 13:03 laxmi2577

I've opened PR #3598 with the lazy import fix.

@quaquel - I went with the second option you mentioned (moving the import inside Network.init) to keep the base installation lightweight. I also added a # noqa: PLC0415 flag to bypass the Ruff eager-import rule for that specific line, which cleared the linter.

@Khalil-Youssefi - Thanks again for catching this and logging the issue!

The fix was tested locally in a sterile environment, and all CI checks are green. Ready for review whenever the team has time.

laxmi2577 avatar Mar 22 '26 14:03 laxmi2577