Skip to content

Commit

Permalink
Switch PythonVirtualenvOperator to venv from virtualenv package
Browse files Browse the repository at this point in the history
The PythonVirtualenvOperator has been using virtualenv in order
to support Python 2.7 and pre Python 3.5, however we do not need
those any more - and we can use built-in venv instead.

Fixes: apache#40420
  • Loading branch information
potiuk committed Oct 31, 2024
1 parent 7506bed commit a0130d6
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 34 deletions.
3 changes: 1 addition & 2 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ Those extras are available as regular core airflow extras - they install optiona
# START CORE EXTRAS HERE

aiobotocore, apache-atlas, apache-webhdfs, async, cgroups, cloudpickle, github-enterprise, google-
auth, graphviz, kerberos, ldap, leveldb, otel, pandas, password, rabbitmq, s3fs, sentry, statsd, uv,
virtualenv
auth, graphviz, kerberos, ldap, leveldb, otel, pandas, password, rabbitmq, s3fs, sentry, statsd, uv

# END CORE EXTRAS HERE

Expand Down
3 changes: 1 addition & 2 deletions contributing-docs/12_airflow_dependencies_and_extras.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ Those extras are available as regular core airflow extras - they install optiona
.. START CORE EXTRAS HERE
aiobotocore, apache-atlas, apache-webhdfs, async, cgroups, cloudpickle, github-enterprise, google-
auth, graphviz, kerberos, ldap, leveldb, otel, pandas, password, rabbitmq, s3fs, sentry, statsd, uv,
virtualenv
auth, graphviz, kerberos, ldap, leveldb, otel, pandas, password, rabbitmq, s3fs, sentry, statsd, uv

.. END CORE EXTRAS HERE
Expand Down
2 changes: 0 additions & 2 deletions docs/apache-airflow/extra-packages-ref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ python dependencies for the provided package.
+---------------------+-----------------------------------------------------+----------------------------------------------------------------------------+
| uv | ``pip install 'apache-airflow[uv]'`` | Install uv - fast, Rust-based package installer (experimental) |
+---------------------+-----------------------------------------------------+----------------------------------------------------------------------------+
| virtualenv | ``pip install 'apache-airflow[virtualenv]'`` | Running python tasks in local virtualenv |
+---------------------+-----------------------------------------------------+----------------------------------------------------------------------------+
| cloudpickle | pip install apache-airflow[cloudpickle] | Cloudpickle hooks and operators |
+---------------------+-----------------------------------------------------+----------------------------------------------------------------------------+

Expand Down
3 changes: 1 addition & 2 deletions generated/provider_dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -1286,8 +1286,7 @@
"standard": {
"deps": [
"apache-airflow-providers-common-sql>=1.18.0",
"apache-airflow>=2.8.0",
"virtualenv>=20.26.0"
"apache-airflow>=2.8.0"
],
"devel-deps": [],
"plugins": [],
Expand Down
3 changes: 0 additions & 3 deletions hatch_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@
"uv": [
"uv>=0.1.32",
],
"virtualenv": [
"virtualenv>=20.26.0",
],
}

DOC_EXTRAS: dict[str, list[str]] = {
Expand Down
1 change: 1 addition & 0 deletions newsfragments/43568.significant.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove ``virtualenv`` extra as PythonVirtualenvOperator has been moved to standard provider and switched to use built-in ven package.
1 change: 1 addition & 0 deletions providers/src/airflow/providers/standard/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ Breaking changes
~~~~~~~~~~~~~~~~

* ``In BranchDayOfWeekOperator, DayOfWeekSensor, BranchDateTimeOperator parameter use_task_execution_date has been removed. Please use use_task_logical_date.``
* ``PythonVirtualenvOperator uses built-in venv instead of virtualenv package.``
14 changes: 0 additions & 14 deletions providers/src/airflow/providers/standard/operators/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# under the License.
from __future__ import annotations

import importlib
import inspect
import json
import logging
Expand Down Expand Up @@ -73,17 +72,6 @@
from airflow.utils.context import Context


def is_venv_installed() -> bool:
"""
Check if the virtualenv package is installed via checking if it is on the path or installed as package.
:return: True if it is. Whichever way of checking it works, is fine.
"""
if shutil.which("virtualenv") or importlib.util.find_spec("virtualenv"):
return True
return False


@cache
def _parse_version_info(text: str) -> tuple[int, int, int, str, int]:
"""Parse python version info from a text."""
Expand Down Expand Up @@ -710,8 +698,6 @@ def __init__(
"Passing non-string types (e.g. int or float) as python_version not supported"
)

if not is_venv_installed():
raise AirflowException("PythonVirtualenvOperator requires virtualenv, please install it.")
if use_airflow_context and (not expect_airflow and not system_site_packages):
error_msg = "use_airflow_context is set to True, but expect_airflow and system_site_packages are set to False."
raise AirflowException(error_msg)
Expand Down
1 change: 0 additions & 1 deletion providers/src/airflow/providers/standard/provider.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ versions:
dependencies:
- apache-airflow>=2.8.0
- apache-airflow-providers-common-sql>=1.18.0
- virtualenv>=20.26.0

integrations:
- integration-name: Standard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
from airflow.utils.process_utils import execute_in_subprocess


def _generate_virtualenv_cmd(tmp_dir: str, python_bin: str, system_site_packages: bool) -> list[str]:
cmd = [sys.executable, "-m", "virtualenv", tmp_dir]
def _generate_venv_cmd(tmp_dir: str, python_bin: str, system_site_packages: bool) -> list[str]:
"""We are using venv command instead of venv module to allow creation of venv for different python versions."""
if python_bin is None:
python_bin = sys.executable
cmd = [python_bin, "-m", "venv", tmp_dir]
if system_site_packages:
cmd.append("--system-site-packages")
if python_bin is not None:
cmd.append(f"--python={python_bin}")
return cmd


Expand Down Expand Up @@ -90,8 +91,8 @@ def prepare_virtualenv(
if index_urls is not None:
_generate_pip_conf(Path(venv_directory) / "pip.conf", index_urls)

virtualenv_cmd = _generate_virtualenv_cmd(venv_directory, python_bin, system_site_packages)
execute_in_subprocess(virtualenv_cmd)
venv_cmd = _generate_venv_cmd(venv_directory, python_bin, system_site_packages)
execute_in_subprocess(venv_cmd)

if requirements is not None and requirements_file_path is not None:
raise ValueError("Either requirements OR requirements_file_path has to be passed, but not both")
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ dynamic = ["version", "optional-dependencies", "dependencies"]
# START CORE EXTRAS HERE
#
# aiobotocore, apache-atlas, apache-webhdfs, async, cgroups, cloudpickle, github-enterprise, google-
# auth, graphviz, kerberos, ldap, leveldb, otel, pandas, password, rabbitmq, s3fs, sentry, statsd, uv,
# virtualenv
# auth, graphviz, kerberos, ldap, leveldb, otel, pandas, password, rabbitmq, s3fs, sentry, statsd, uv
#
# END CORE EXTRAS HERE
#
Expand Down

0 comments on commit a0130d6

Please sign in to comment.