Skip to content

Fix: Lazy import networkx in Network space (#3596) - #3598

Merged
quaquel merged 7 commits into
mesa:mainfrom
laxmi2577:fix-3596-lazy-networkx-import
Mar 29, 2026
Merged

quaquel merged 7 commits into
mesa:mainfrom
laxmi2577:fix-3596-lazy-networkx-import

Conversation

@laxmi2577

Copy link
Copy Markdown
Contributor

Description

This PR resolves the module crash for users utilizing the base mesa installation. It addresses the eager import issue raised in #3596 and aligns with the broader lazy-import architecture discussed in #2343.

Changes made:

  • Removed the global import networkx as nx from mesa/discrete_space/network.py.
  • Updated the Network.__init__ signature so layout defaults to None.
  • Implemented a lazy import of networkx directly inside __init__, ensuring it only executes when a Network grid is explicitly instantiated.

Testing

  • Created a sterile virtual environment.
  • Installed Mesa without optional dependencies (pip install -e .).
  • Verified that import mesa.discrete_space executes successfully without throwing a ModuleNotFoundError for networkx.

Fixes #3596
Related to #2343

Copilot AI review requested due to automatic review settings March 22, 2026 13:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an import-time crash when using the base mesa installation by removing the eager networkx dependency from the mesa.discrete_space import path, aligning with Mesa’s lazy-import approach for optional dependencies.

Changes:

  • Removed module-level networkx import from mesa/discrete_space/network.py.
  • Changed Network.__init__ so layout defaults to None and is set to nx.circular_layout at runtime.
  • Added a lazy networkx import inside Network.__init__ to defer the dependency until Network is instantiated.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread mesa/discrete_space/network.py Outdated
Comment on lines +51 to +53
import networkx as nx

if layout is None:

Copilot AI Mar 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lazy import currently happens unconditionally, so instantiating Network will try to import networkx even when the caller provides an explicit layout (e.g., a precomputed Mapping). Consider moving the networkx import inside the if layout is None: block (and, optionally, catching ModuleNotFoundError to raise a clearer message about installing the mesa[network] extra).

Suggested change
import networkx as nx
if layout is None:
if layout is None:
try:
import networkx as nx
except ModuleNotFoundError as e:
raise ModuleNotFoundError(
"Network layout generation requires the 'networkx' package. "
"Install mesa with the 'network' extra, e.g.: pip install \"mesa[network]\""
) from e

Copilot uses AI. Check for mistakes.
Comment thread mesa/discrete_space/network.py Outdated
Comment on lines +51 to +55
import networkx as nx

if layout is None:
layout = nx.circular_layout

Copilot AI Mar 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is trailing whitespace on the blank lines around the import networkx as nx / layout defaulting block. This can cause formatter/lint noise; please remove the whitespace-only indentation on those blank lines.

Copilot uses AI. Check for mistakes.
Comment thread mesa/discrete_space/network.py Outdated
Comment on lines +51 to +53
import networkx as nx

if layout is None:

Copilot AI Mar 22, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is specifically intended to allow importing mesa.discrete_space without networkx installed. Consider adding a unit test that simulates networkx being unavailable (e.g., by patching import machinery) and asserts that importing mesa.discrete_space succeeds, so the regression is caught in CI.

Suggested change
import networkx as nx
if layout is None:
if layout is None:
try:
import networkx as nx
except ImportError as e:
raise ImportError(
"NetworkX is required when no 'layout' is provided. "
"Install 'networkx' or pass a layout callable or mapping."
) from e

Copilot uses AI. Check for mistakes.
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.6% [-2.0%, -1.3%] 🔵 -2.1% [-2.2%, -2.0%]
BoltzmannWealth large 🔵 -1.2% [-1.6%, -0.8%] 🟢 -5.7% [-6.7%, -4.6%]
Schelling small 🔵 -0.1% [-0.5%, +0.2%] 🔵 -0.4% [-1.1%, +0.4%]
Schelling large 🔵 -0.1% [-0.7%, +0.6%] 🔵 -0.9% [-1.9%, +0.2%]
WolfSheep small 🔵 -0.4% [-0.6%, -0.1%] 🔵 +2.8% [-1.9%, +7.9%]
WolfSheep large 🔵 -0.6% [-1.5%, +0.3%] 🔵 -1.7% [-3.0%, -0.2%]
SugarscapeG1mt small 🔵 -0.4% [-0.6%, -0.2%] 🔵 -0.1% [-1.6%, +1.3%]
SugarscapeG1mt large 🔵 +0.5% [+0.0%, +0.9%] 🔵 -1.3% [-3.3%, +0.8%]
BoidFlockers small 🔵 +0.3% [-0.0%, +0.7%] 🔵 +0.9% [+0.4%, +1.3%]
BoidFlockers large 🔵 +0.2% [-0.3%, +0.9%] 🔵 +0.2% [-0.1%, +0.5%]

@quaquel

quaquel commented Mar 22, 2026

Copy link
Copy Markdown
Member

Copilot has some useful suggestions that you might want to include.

@quaquel quaquel added dependencies Pull requests that update a dependency file depedency labels Mar 22, 2026
Comment thread mesa/discrete_space/network.py Resolved
@quaquel
quaquel merged commit c7b56cd into mesa:main Mar 29, 2026
8 of 9 checks passed
codebreaker32 pushed a commit to codebreaker32/mesa that referenced this pull request Apr 1, 2026
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
Backport of #3598 (fixes #3596), adapted for 3.5.x. NetworkX is an optional
dependency, but mesa.discrete_space.network imported it at module level, so
'import mesa' failed without it.

On main the default layout became None (resolved to circular_layout). In
3.5.x layout=None means a purely topological network and the default is
spring_layout, so this keeps both and wraps spring_layout in a small helper
that imports networkx when called.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

networkx is not in the dependencies

3 participants