Skip to content

Commit

Permalink
Fix check if virtualenv is installed in PythonVirtualenvOperator (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shahar1 authored Jul 30, 2023
1 parent 16e0830 commit ddcd474
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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

0 comments on commit ddcd474

Please sign in to comment.