Versioned Python scripts

State the BornAgain version and package requirements in each script, and run it in an environment that satisfies those requirements.

Runtime guard

Scripts shipped with BornAgain check the installed version right after the import:

import bornagain as ba
ba.require_versions("bornagain>=25,<26")

ba.require_versions accepts any number of PEP 508 requirement strings, e.g. "bornagain>=25,<26", "python>=3.10", "numpy>=1.26.0". If a script needs additional packages, add these requirements explicitly. Formatting does not matter; this can be written on one line:

import bornagain as ba
ba.require_versions(
    "bornagain>=25,<26", "python>=3.10", "numpy>=1.26.0", "scipy>=1.14.1"
)

or split over several lines:

import bornagain as ba
ba.require_versions(
    "bornagain>=25,<26",
    "python>=3.10",
    "numpy>=1.26.0",
    "scipy>=1.14.1",
)

If a requirement is not met, the script stops immediately with an ImportError that names the installed and the required version. Use the same guard in your own scripts.

Run in a virtual environment

Create a virtual environment and install the script’s requirements in it:

python -m venv ~/.venvs/ba25
~/.venvs/ba25/bin/python -m pip install "bornagain>=25,<26"

Run the script with the same interpreter:

~/.venvs/ba25/bin/python my_script.py

Use this environment’s interpreter for both package installation and script execution. Install any additional requirements declared by the script in the same environment.

Self-installing scripts with uv

Scripts shipped with BornAgain also declare their requirements in a PEP 723 comment block:

# /// script
# requires-python = ">=3.10"
# dependencies = ["bornagain>=25,<26"]
# ///

Users of uv then get a matching environment created on the fly, without any manual setup:

uv run my_script.py