Repository navigation
Fix: Lazy import networkx in Network space (#3596) - #3598
Conversation
There was a problem hiding this comment.
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
networkximport frommesa/discrete_space/network.py. - Changed
Network.__init__solayoutdefaults toNoneand is set tonx.circular_layoutat runtime. - Added a lazy
networkximport insideNetwork.__init__to defer the dependency untilNetworkis instantiated.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import networkx as nx | ||
|
|
||
| if layout is None: |
There was a problem hiding this comment.
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).
| 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 |
| import networkx as nx | ||
|
|
||
| if layout is None: | ||
| layout = nx.circular_layout | ||
|
|
There was a problem hiding this comment.
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.
| import networkx as nx | ||
|
|
||
| if layout is None: |
There was a problem hiding this comment.
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.
| 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 |
|
Performance benchmarks:
|
|
Copilot has some useful suggestions that you might want to include. |
Co-authored-by: Jan Kwakkel <[email protected]>
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.
Description
This PR resolves the module crash for users utilizing the base
mesainstallation. It addresses the eager import issue raised in #3596 and aligns with the broader lazy-import architecture discussed in #2343.Changes made:
import networkx as nxfrommesa/discrete_space/network.py.Network.__init__signature solayoutdefaults toNone.networkxdirectly inside__init__, ensuring it only executes when aNetworkgrid is explicitly instantiated.Testing
pip install -e .).import mesa.discrete_spaceexecutes successfully without throwing aModuleNotFoundErrorfornetworkx.Fixes #3596
Related to #2343