Skip to content

utils

_init_venv_v1(*, path, python_version=None, logger_name) async

Set a virtual environment at path/venv

Parameters:

Name Type Description Default
path

Path path to directory in which to set up the virtual environment

required
python_version

default=None Python version the virtual environment will be based upon

None

Returns:

Name Type Description
python_bin Path

Path path to python interpreter

Source code in fractal_server/tasks/v1/utils.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
async def _init_venv_v1(
    *,
    path: Path,
    python_version: Optional[str] = None,
    logger_name: str,
) -> Path:
    """
    Set a virtual environment at `path/venv`

    Args:
        path : Path
            path to directory in which to set up the virtual environment
        python_version : default=None
            Python version the virtual environment will be based upon

    Returns:
        python_bin : Path
            path to python interpreter
    """
    logger = get_logger(logger_name)
    logger.debug(f"[_init_venv] {path=}")
    interpreter = get_python_interpreter_v1(version=python_version)
    logger.debug(f"[_init_venv] {interpreter=}")
    await execute_command_async(
        cwd=path,
        command=f"{interpreter} -m venv venv",
        logger_name=logger_name,
    )
    python_bin = path / "venv/bin/python"
    logger.debug(f"[_init_venv] {python_bin=}")
    return python_bin

get_python_interpreter_v1(version=None)

Return the path to the python interpreter

Parameters:

Name Type Description Default
version Optional[str]

Python version

None

Raises:

Type Description
ValueError

If the python version requested is not available on the host.

Returns:

Name Type Description
interpreter str

string representing the python executable or its path

Source code in fractal_server/tasks/v1/utils.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def get_python_interpreter_v1(version: Optional[str] = None) -> str:
    """
    Return the path to the python interpreter

    Args:
        version: Python version

    Raises:
        ValueError: If the python version requested is not available on the
                    host.

    Returns:
        interpreter: string representing the python executable or its path
    """
    import shutil
    import sys

    if version:
        interpreter = shutil.which(f"python{version}")
        if not interpreter:
            raise ValueError(
                f"Python version {version} not available on host."
            )
    else:
        interpreter = sys.executable

    return interpreter