conda_subprocess.decorator.conda

Contents

conda_subprocess.decorator.conda#

conda_subprocess.decorator.conda(prefix_name: str | None = None, prefix_path: str | None = None, hostname_localhost: bool = False)[source]#

Decorator to execute a python function in a different conda environment, built on top of executorlib.

The decorated function (including its arguments and return value) is shipped to a worker process running in the target conda environment via cloudpickle, so the target environment’s Python minor version must match the one used to define the decorated function.

Parameters:
  • prefix_name (str) – Name of the conda environment the function should be executed in, e.g. “py312”.

  • prefix_path (str) – Absolute path of the conda environment the function should be executed in.

  • hostname_localhost (bool) – Use localhost instead of the system hostname to establish the connection to the worker process. Required in restricted network environments such as containers or HPC login nodes where the hostname cannot be resolved.

Returns:

Decorator which wraps the given function so it executes in the

target conda environment.

Return type:

Callable

Example

>>> from conda_subprocess.decorator import conda
>>> @conda(prefix_name="py312")
... def add_function(parameter_1, parameter_2):
...     return parameter_1 + parameter_2
>>> add_function(parameter_1=1, parameter_2=2)
3