Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitk::PythonContext Class Reference

Provides a Python interpreter context for executing Python code and interacting with MITK data like images. More...

#include <mitkPythonContext.h>

Public Member Functions

 PythonContext (const std::string &venvName={})
 Constructs a PythonContext and optionally creates or activates a virtual environment. More...
 
 ~PythonContext ()
 Destructor. Clears the internal Python dictionary. More...
 
void Activate (bool importBindings=true)
 Initializes the Python interpreter context and sets up module paths. More...
 
bool HasVariable (const std::string &varName)
 Checks whether a Python variable with the given name exists. More...
 
std::optional< bool > GetVariableAsBool (const std::string &varName)
 Retrieves a Python variable as a bool. More...
 
std::optional< int > GetVariableAsInt (const std::string &varName)
 Retrieves a Python variable as an int. More...
 
std::optional< double > GetVariableAsDouble (const std::string &varName)
 Retrieves a Python variable as a double. More...
 
std::optional< std::string > GetVariableAsString (const std::string &varName)
 Retrieves a Python variable as a string. More...
 
void BindImage (mitk::Image *image, const std::string &varName)
 Binds an MITK image to a Python variable in the context's dictionary. More...
 
void Execute (const std::string &expression)
 Executes arbitrary Python code within this context. More...
 
void ExecuteFile (const fs::path &filePath)
 Executes a Python file within this context. More...
 

Detailed Description

Provides a Python interpreter context for executing Python code and interacting with MITK data like images.

This class wraps a Python interpreter and maintains a dictionary for executing Python code. It allows binding MITK data like images to Python variables and retrieving Python variables from the context in a type-safe manner.

A typical usage pattern is:

mitk::PythonContext ctx("myenv");
ctx.Activate();
ctx.BindImage(image, "input_image");
ctx.Execute("result = process(input_image)");
auto value = ctx.GetVariableAsInt("result");
Provides a Python interpreter context for executing Python code and interacting with MITK data like i...
See also
mitk::nnInteractiveTool

Definition at line 47 of file mitkPythonContext.h.

Constructor & Destructor Documentation

◆ PythonContext()

mitk::PythonContext::PythonContext ( const std::string &  venvName = {})
explicit

Constructs a PythonContext and optionally creates or activates a virtual environment.

If the Python interpreter has not been initialized yet, it will be initialized during construction.

Parameters
[in]venvNameName of the Python virtual environment to create or activate. If empty, no virtual environment is used.
Exceptions
mitk::Exceptionif the virtual environment cannot be created or activated.
Postcondition
The Python interpreter is initialized.

◆ ~PythonContext()

mitk::PythonContext::~PythonContext ( )

Destructor. Clears the internal Python dictionary.

Member Function Documentation

◆ Activate()

void mitk::PythonContext::Activate ( bool  importBindings = true)

Initializes the Python interpreter context and sets up module paths.

Always adds the base interpreter's and the active virtual environment's site-packages to sys.path, so a distribution installed in the venv is importable and its metadata is readable. With importBindings (the default) it additionally imports NumPy and the MITK Python module for data exchange.

Pass false to keep the context free of any venv native library. On Linux the activated venv becomes sys.prefix, so its site-packages precede the base on sys.path and importing NumPy would load it (and its compiled extensions) from the venv. A later "is any venv module loaded?" check would then report true and block an in-place update or uninstall. Metadata-only work (version and install probes) must therefore activate without bindings.

Parameters
[in]importBindingsImport NumPy and the MITK module when true.
Precondition
The PythonContext has been constructed.
Exceptions
mitk::Exceptionif any of the Python initialization commands fail.

◆ BindImage()

void mitk::PythonContext::BindImage ( mitk::Image *  image,
const std::string &  varName 
)

Binds an MITK image to a Python variable in the context's dictionary.

The image is passed by reference to Python (no copy). If the image pointer is nullptr, the variable is set to Python's None.

Parameters
[in]imagePointer to the Image to bind. If nullptr, the variable is set to None.
[in]varNameName of the Python variable to assign the image to.
Exceptions
mitk::Exceptionif the image cannot be bound to the variable.

◆ Execute()

void mitk::PythonContext::Execute ( const std::string &  expression)

Executes arbitrary Python code within this context.

The code is executed using the context's dictionary, so variables set in previous calls are available in subsequent ones.

Parameters
[in]expressionThe Python code to execute. May contain multiple statements separated by newlines.
Exceptions
mitk::Exceptionif execution fails (e.g., due to a Python error).

◆ ExecuteFile()

void mitk::PythonContext::ExecuteFile ( const fs::path &  filePath)

Executes a Python file within this context.

The file contents are executed using the same dictionary as Execute(), so variables and imports remain available across calls. The special file variable is set to the executed file path.

Parameters
[in]filePathAbsolute or relative path to the Python file.
Exceptions
mitk::Exceptionif the file cannot be read or execution fails.

◆ GetVariableAsBool()

std::optional<bool> mitk::PythonContext::GetVariableAsBool ( const std::string &  varName)

Retrieves a Python variable as a bool.

Parameters
[in]varNameName of the Python variable.
Returns
The variable value if it exists and can be cast to bool, std::nullopt otherwise.

◆ GetVariableAsDouble()

std::optional<double> mitk::PythonContext::GetVariableAsDouble ( const std::string &  varName)

Retrieves a Python variable as a double.

Parameters
[in]varNameName of the Python variable.
Returns
The variable value if it exists and can be cast to double, std::nullopt otherwise.

◆ GetVariableAsInt()

std::optional<int> mitk::PythonContext::GetVariableAsInt ( const std::string &  varName)

Retrieves a Python variable as an int.

Parameters
[in]varNameName of the Python variable.
Returns
The variable value if it exists and can be cast to int, std::nullopt otherwise.

◆ GetVariableAsString()

std::optional<std::string> mitk::PythonContext::GetVariableAsString ( const std::string &  varName)

Retrieves a Python variable as a string.

Parameters
[in]varNameName of the Python variable.
Returns
The variable value if it exists and can be cast to std::string, std::nullopt otherwise.

◆ HasVariable()

bool mitk::PythonContext::HasVariable ( const std::string &  varName)

Checks whether a Python variable with the given name exists.

Parameters
[in]varNameName of the Python variable to check.
Returns
true if the variable exists in the context's dictionary, false otherwise.

The documentation for this class was generated from the following file: