Skip to main content

PythonFMU3

Turn a Python class into an FMI 3.0 FMU that Simulink, OMSimulator, FMPy, fmusim and other simulation tools can import.

License: MIT CI PyPI Downloads conda-forge Read the Docs Listed on fmi-standard.org

Try it in your browser, no install: PythonFMU3 Studio on the web lets you write a model, simulate it and download the FMU. It has an AI assistant that writes and tests models for you, and you can share a model as a link.

Highlights

  • Listed on the FMI tools page at fmi-standard.org.
  • Runs in any FMI 3.0 importer: Python-based tools such as FMPy, and native tools such as Simulink, OMSimulator and fmusim.
  • Variables declared like dataclass fields: T: float = Output(293.15, unit="K"), with units, arrays and Model Exchange states handled for you (Declaring variables); pythonfmu3 migrate converts existing models.
  • Co-Simulation and Model Exchange, with events, arrays, structural parameters, units, enumerations and FMU state.
  • PythonFMU3 Studio: pythonfmu3 studio opens a local app to write, check, simulate, build and inspect FMUs, with live plots and model templates. There is also a web version.
  • Jupyter notebooks: write a model in a cell with %%fmu, simulate it into a pandas DataFrame, build the FMU.
  • AI agents: pythonfmu3 mcp lets Claude, GitHub Copilot and Cursor write, check, simulate and build FMUs. The docs also publish llms.txt so AI assistants get the API right.
  • Examples tested in FMPy and fmusim, including 1D pipe flow for conjugate heat transfer.
  • Coming from PythonFMU (FMI 2.0)? See the migration guide.

See the documentation for the full feature list and current limitations, and the changelog for what changed in each release.

Where can I run the FMU?

In any FMI 3.0 importer: Python-based tools such as FMPy, and native tools such as OMSimulator, fmusim or Simulink.

The machine running the simulation needs Python 3 with a shared Python library, plus the packages your model imports (see pythonfmu3 deploy below). Inside a Python-based importer the FMU uses that interpreter. Otherwise, on Linux and macOS the FMU finds and loads libpython itself; if it picks the wrong one, or none, point it at the right library:

export PYTHONFMU_PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.12.so.1.0

On Windows, python3.dll must be on the PATH. See Running FMUs outside Python for the full lookup order.

PythonFMU3 Studio

pythonfmu3 studio opens a local web app to write, check, simulate, build and inspect FMUs, with live plots, a model-description preview, an FMU inspector and templates. It needs no extra dependencies. See the Studio docs.

PythonFMU3 Studio

Jupyter notebooks

Write a model in a notebook cell with %%fmu, check it, simulate it into a pandas DataFrame and build the FMU:

%load_ext pythonfmu3
# %%fmu cell with your class, then:
res = Tank.simulate(stop_time=100)
fmu = Tank.build()

See Jupyter notebooks and the example notebook, which also runs in Google Colab.

AI agents (MCP)

pythonfmu3 mcp lets AI agents (Claude, GitHub Copilot in VS Code, Cursor, ...) write, check, simulate and build FMUs for you:

pip install "pythonfmu3[mcp]"
claude mcp add pythonfmu3 -- pythonfmu3 mcp ~/fmu-models

Then ask, for example: "Make me a bouncing ball FMU and simulate it for 5 s." See AI agents (MCP).

How do I build an FMU from python code?

  1. Install pythonfmu3 package:
pip install pythonfmu3
  1. Create a class extending Fmi3Slave (see below for an example). Keep a single FMU class per script.
  2. Run pythonfmu3 build to create the fmu.
usage: pythonfmu3 build [-h] -f SCRIPT_FILE [-d DEST] [--doc DOCUMENTATION_FOLDER] [--terminals TERMINALS]
                        [--no-external-tool] [--only-one-per-process] [--handle-state] [--serialize-state]
                        [--no-variable-step] [--needs-completed-step]
                        [Project files ...]

Build an FMU from a Python script.

positional arguments:
  Project files         Additional project files required by the Python script.

options:
  -h, --help            show this help message and exit
  -f SCRIPT_FILE, --file SCRIPT_FILE
                        Path to the Python script.
  -d DEST, --dest DEST  Where to save the FMU.
  --doc DOCUMENTATION_FOLDER
                        Documentation folder to include in the FMU.
  --terminals TERMINALS
                        Terminals file (terminalsAndIcons.xml) to include in the FMU.
  --no-external-tool    If given, needsExecutionTool=false
  --only-one-per-process
                        If given, canBeInstantiatedOnlyOncePerProcess=true
  --handle-state        If given, canGetAndSetFMUState=true
  --serialize-state     If given, canSerializeFMUState=true
  --no-variable-step    If given, canHandleVariableCommunicationStepSize=false
  --needs-completed-step
                        If given, needsCompletedIntegratorStep=true

How do I build an FMU from python code with third-party dependencies?

Often, Python scripts depends on non-builtin libraries like numpy, scipy, etc. PythonFMU3 does not package a full environment within the FMU. However, you can package a requirements.txt or environment.yml file within your FMU following these steps:

  1. Install the pythonfmu3 package: pip install pythonfmu3
  2. Create a class extending Fmi3Slave (see the examples below).
  3. Create a requirements.txt file (to use pip manager) and/or a environment.yml file (to use conda manager) that defines your dependencies.
  4. Run pythonfmu3 build -f myscript.py requirements.txt to create the fmu including the dependencies file.

And using pythonfmu3 deploy, end users will be able to update their local Python environment. The steps to achieve that:

  1. Install the pythonfmu3 package: pip install pythonfmu3
  2. Be sure to be in the Python environment to be updated. Then execute pythonfmu3 deploy -f my.fmu
usage: pythonfmu3 deploy [-h] -f FMU [-e ENVIRONMENT] [{pip,conda}]

Deploy a Python FMU. The command will look in the `resources` folder for one of the following files:
`requirements.txt` or `environment.yml`. If you specify a environment file but no package manager, `conda` will be selected for `.yaml` and `.yml` otherwise `pip` will be used. The tool assume the Python environment in which the FMU should be executed is the current one.

positional arguments:
  {pip,conda}           Python packages manager

optional arguments:
  -h, --help            show this help message and exit
  -f FMU, --file FMU    Path to the Python FMU.
  -e ENVIRONMENT, --env ENVIRONMENT
                        Requirements or environment file.

Cosimulation Example:

Write the script

from pythonfmu3 import Fmi3Slave, Input, Local, Output, Parameter


class PythonSlave(Fmi3Slave):
    author = "John Doe"
    description = "A simple description"

    realIn: float = Input(1.0)
    gain: float = Parameter(3.0)
    realOut: float = Output(3.0)
    intOut: int = Output(1)
    booleanVariable: bool = Local(True)
    stringVariable: str = Local("Hello World!")

    def do_step(self, current_time, step_size):
        self.realOut = self.gain * self.realIn
        return True

Each class-level line declares one FMU variable, and time is added for you; see Declaring variables. The explicit style, calling register_variable in __init__, still works and can be mixed in, and pythonfmu3 migrate pythonslave.py converts an existing model to declarations.

Create the FMU

pythonfmu3 build -f pythonslave.py myproject

In this example a python class named PythonSlave that extends Fmi3Slave is declared in a file named pythonslave.py, where myproject is an optional folder containing additional project files required by the python script. Project folders such as this will be recursively copied into the FMU. Multiple project files/folders may be added.

Model Exchange Example

To create a model exchange FMU you must inherit from Fmi3SlaveBase and one of (or both) mixin classes ModelExchange or CoSimulation.

Write the script

from typing import List

from pythonfmu3 import Fmi3SlaveBase, ModelExchange, Parameter, State


class PythonSlaveMX(Fmi3SlaveBase, ModelExchange):
    author = "John Doe"
    description = "Van der Pol oscillator"

    x0: float = State(2.0)
    x1: float = State(0.0)
    mu: float = Parameter(1.0)

    def get_continuous_state_derivatives(self) -> List[float]:
        self.der_x0 = self.x1
        self.der_x1 = self.mu * ((1 - self.x0**2) * self.x1) - self.x0
        return [self.der_x0, self.der_x1]

State declares a continuous state with its derivative, here der(x0) and der(x1), which your methods set as self.der_x0 and self.der_x1. time is registered for you.

Create the FMU

pythonfmu3 build -f pythonslaveMX.py

Credits

PythonFMU3 is a fork of PythonFMU, which supports FMI 2.0, extended to FMI 3.0. Many thanks to the PythonFMU authors for the foundation this project is built on.

Metadata

Release files for pythonfmu3 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pythonfmu3 1.4.0
File Size Uploaded Provenance Actions
pythonfmu3-1.4.0.tar.gz 687.8 kB Without provenance Details for pythonfmu3-1.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pythonfmu3 1.4.0
File Interpreter ABI Platform Actions
pythonfmu3-1.4.0-py3-none-any.whl Python 3 none any Details for pythonfmu3-1.4.0-py3-none-any.whl

Total release size: 1.4 MB

Release files / pythonfmu3-1.4.0.tar.gz

Download URL pythonfmu3-1.4.0.tar.gz
Size 687.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6f456bf0886f12cec4ccb2cbfe8c71ab437477470c9bc2854c865153e49576f4
BLAKE2b-256 checksum
How to use checksums
181e0e5bc057ca61107872856b354dc6d537159708968d6a9211645ebc0cb5c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Release files / pythonfmu3-1.4.0-py3-none-any.whl

Download URL pythonfmu3-1.4.0-py3-none-any.whl
Size 716.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
854d7ae549f6794c62270a9821c20d2f8e8923f147c8486d5241089142883975
BLAKE2b-256 checksum
How to use checksums
c0a203cc410c529de695650f33caeec7881396b212a7493189650b9edc946296
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Security for pythonfmu3 1.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.4.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page