apsimNGpy is a cutting-edge, open-source framework for advanced agroecosystem modeling, built entirely in Python. It enables object-oriented, data-driven workflows for interacting with APSIM Next Generation models, offering capabilities for:
- Batch file simulation and model evaluation
- APSIMX file editing and parameter inspection
- Weather data retrieval and pre-processing
- Optimization and performance diagnostics
- Efficient soil profile development and validation
Python serves as the execution environment, integrating scientific computing, data analysis, and automation for sustainable agricultural systems.
---
- .NET SDK — install from https://learn.microsoft.com/en-us/dotnet/core/install/
- Python 3.10+
- APSIM Next Generation — ensure the directory containing
Models.exeis added to your system PATH. - (Optional) Use the official APSIM installer for easiest setup.
- Minimum 8 GB RAM recommended.
---
Option 1 – Install from PyPI (stable)
pip install apsimNGpyIf using the uv virtual environment manager:
uv pip install apsimNGpyOption 2 – Clone the development repository
git clone https://github.com/MAGALA-RICHARD/apsimNGpy.git
cd apsimNGpy
pip install .Option 3 – Install directly from GitHub
pip install git+https://github.com/MAGALA-RICHARD/apsimNGpy.gitUse the pinned APSIM release indicated on the documentation homepage to avoid forward-compatibility issues. The pinned version represents the latest APSIM NG build verified against apsimNGpy’s API and unit tests.
This release introduces a dedicated simulation file context manager that automatically removes transient .apsimx files and all associated output database files (.db, .db-wal, .db-sha, etc.) at the end of a with block. This lets users run temporary simulations with confidence that intermediate files will not accumulate on disk — especially important for large workflows such as optimization loops, Monte-Carlo runs, or multi-site batch simulations.
To achieve this, the context manager now coordinates more carefully with APSIM-NG’s I/O locking behavior. In particular, it triggers the underlying C# garbage collector (when necessary) to ensure the write-locks on the result database are fully released before deletion. Because of that, there is a small (but practically negligible) performance overhead; future versions may expose a switch to disable this behaviour for users who do not require automatic cleanup.
This release also adds a second context manager for temporary APSIM bin-path management. It allows users to specify a local APSIM-NG installation path for a given script/module while preserving the global default in memory — enabling cleaner multi-version testing or workflow portability without rewriting environment variables.
from apsimNGpy.core.apsim import ApsimModel
with Apsim_model("template.apsimx") as temp_model:
temp_model.run() # temporary .apsimx + .db files created
df= temp_model.results
print(df)Immediately after exiting the with block, the temporary .apsimx file (and its associated .db files) are deleted, since only clones of the original model file are used inside the context.
from apsimNGpy.core.config import apsim_bin_context
with apsim_bin_context("C:/APSIM/2025.05.1234/bin"):
from Models.Core import Simulations # uses this bin path for loading
from apsimNGpy.core.apsim import ApsimModelImmediately after exiting the with block, the path is restored back to the global APSIM path — meaning that other projects and modules can continue to access their own settings normally. The importance of this pattern is reproducibility: it allows your project to be “locked” to a specific APSIM binary path. APSIM versions change frequently, and a future run of your current project might fail or give different results if a newer APSIM version is picked up without you realizing it. By scoping a local APSIM bin path for this project, you ensure that reruns in the future use exactly the same APSIM version that generated the original results. This makes the workflow both reproducible and stable.
Note
Since the model assemblies are already loaded into memory inside the apsim_bin_context, you do not need to remain inside the with block to keep using them. Once loaded, those modules (and their namespaces) are global within the process, and they retain their reference to the APSIM bin path that was supplied during loading.
The bin path can also be substituted with just the project .env path as follows
with apsim_bin_context( dotenv_path = './config/.env', bin_key ='APSIM_BIN_PATH'):
from Models.Core import Simulations # uses this bin path for loading
from apsimNGpy.core.apsim import ApsimModel # uses this bin path for loadingpreview_simulation(watch=True)now supports interactive edit syncing: - Opens the.apsimxfile in the APSIM NG GUI and watches for saved edits. - When a user saves in the GUI, changes are automatically reloaded into the activeApsimModelinstance. - Console messages guide users during the live session (e.g., “Watching for GUI edits... Save in APSIM to sync back.”). - Graceful shutdown supported viaCtrl+C; after termination, the Python model reflects the most recent GUI state. - Users should close the GUI after completing edits before continuing with the Python model instance.
- This feature is experimental but stable in tests.
- Synchronization assumes that both APSIM GUI and Python edit the same
.apsimxfile path. - If
watch=False(default),preview_simulationbehaves as before — no live syncing. - GUI edits must be saved before synchronization occurs. Unsaved edits are ignored.
- New function signature:
preview_simulation(self, watch=False) - Existing scripts calling
preview_simulation()remain fully compatible. - File-watching currently uses file modification times; future releases may support event-based detection.
This build is stable for day-to-day work, with incremental API refinements.
- Updated
savemethod onApsimModelto include areloadparameter. - Improved documentation navigation and linked related APIs.