Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check if virtualenv is installed in PythonVirtualenvOperator #32939

Merged
merged 1 commit into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions airflow/operators/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
# under the License.
from __future__ import annotations

import importlib
import inspect
import logging
import os
import pickle
import shutil
import subprocess
import sys
import types
Expand Down Expand Up @@ -540,7 +540,7 @@ def __init__(
"major versions for PythonVirtualenvOperator. Please use string_args."
f"Sys version: {sys.version_info}. Venv version: {python_version}"
)
if not shutil.which("virtualenv"):
if importlib.util.find_spec("virtualenv") is None:
raise AirflowException("PythonVirtualenvOperator requires virtualenv, please install it.")
if not requirements:
self.requirements: list[str] | str = []
Expand Down
10 changes: 10 additions & 0 deletions tests/operators/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,16 @@ def default_kwargs(*, python_version=sys.version_info[0], **kwargs):
kwargs["python_version"] = python_version
return kwargs

@mock.patch("airflow.operators.python.importlib")
def test_virtuenv_not_installed(self, importlib):
importlib.util.find_spec.return_value = None
with pytest.raises(AirflowException, match="requires virtualenv"):

def f():
pass

self.run_as_task(f)

def test_add_dill(self):
def f():
"""Ensure dill is correctly installed."""
Expand Down