From 32cbc1e95256b9ea614c8db975dca30683432db6 Mon Sep 17 00:00:00 2001 From: Joe Scott <17139430+thisiswhereitype@users.noreply.github.com> Date: Sat, 26 Oct 2024 17:36:35 +0100 Subject: [PATCH 1/3] core operator python: check virtualenv is importable by importing This imports the module to check for `ModuleNotFoundError`. A soft import is possible but then we importlib anyway and this will be most robust. --- airflow/operators/python.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/airflow/operators/python.py b/airflow/operators/python.py index b032b45ed3e6e..74c0395db0b53 100644 --- a/airflow/operators/python.py +++ b/airflow/operators/python.py @@ -68,13 +68,16 @@ def is_venv_installed() -> bool: """ - Check if the virtualenv package is installed via checking if it is on the path or installed as package. + Check if the virtualenv package is installed by importing `venv`. - :return: True if it is. Whichever way of checking it works, is fine. + :return: True if no `ModuleNotFoundError` is raised, False otherwise. """ - if shutil.which("virtualenv") or importlib.util.find_spec("virtualenv"): + try: + import venv return True - return False + except ModuleNotFoundError: + return False + def task(python_callable: Callable | None = None, multiple_outputs: bool | None = None, **kwargs): From 23651fb7a58bf1bbf99c77b65c07265cc199549f Mon Sep 17 00:00:00 2001 From: Joe Scott <17139430+thisiswhereitype@users.noreply.github.com> Date: Sun, 27 Oct 2024 14:10:16 +0000 Subject: [PATCH 2/3] Import `virtualenv` not `venv` --- airflow/operators/python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/airflow/operators/python.py b/airflow/operators/python.py index 74c0395db0b53..184d895af5290 100644 --- a/airflow/operators/python.py +++ b/airflow/operators/python.py @@ -73,7 +73,7 @@ def is_venv_installed() -> bool: :return: True if no `ModuleNotFoundError` is raised, False otherwise. """ try: - import venv + import virtualenv return True except ModuleNotFoundError: return False From f8964660601517f2fa0e4bed74f3803ef66f8082 Mon Sep 17 00:00:00 2001 From: Joe Scott <17139430+thisiswhereitype@users.noreply.github.com> Date: Sun, 27 Oct 2024 14:17:24 +0000 Subject: [PATCH 3/3] Docstring `venv` -> `virtualenv` --- airflow/operators/python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/airflow/operators/python.py b/airflow/operators/python.py index 184d895af5290..de795bf8dd43e 100644 --- a/airflow/operators/python.py +++ b/airflow/operators/python.py @@ -68,7 +68,7 @@ def is_venv_installed() -> bool: """ - Check if the virtualenv package is installed by importing `venv`. + Check if the virtualenv package is installed by importing `virtualenv`. :return: True if no `ModuleNotFoundError` is raised, False otherwise. """