From 695752d46cfc8db2d6582c3699980722560c9b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Tue, 22 Sep 2026 15:17:26 +0200 Subject: [PATCH 01/13] WIP: GH-38536: [Python][Packaging] PoC to split libarrow_s3 into its own wheel --- ci/scripts/python_wheel_unix_test.sh | 3 +- ci/scripts/python_wheel_validate_contents.py | 3 +- ci/scripts/python_wheel_xlinux_build.sh | 9 +++- python/CMakeLists.txt | 8 +--- python/pyarrow-s3/CMakeLists.txt | 31 +++++++++++++ python/pyarrow-s3/pyarrow_s3/__init__.py | 31 +++++++++++++ python/pyarrow-s3/pyproject.toml | 47 ++++++++++++++++++++ python/pyarrow/fs.py | 24 +++++++--- 8 files changed, 142 insertions(+), 14 deletions(-) create mode 100644 python/pyarrow-s3/CMakeLists.txt create mode 100644 python/pyarrow-s3/pyarrow_s3/__init__.py create mode 100644 python/pyarrow-s3/pyproject.toml diff --git a/ci/scripts/python_wheel_unix_test.sh b/ci/scripts/python_wheel_unix_test.sh index cb445611e233..04d0400ee033 100755 --- a/ci/scripts/python_wheel_unix_test.sh +++ b/ci/scripts/python_wheel_unix_test.sh @@ -78,7 +78,8 @@ import pyarrow.parquet python -c "import pyarrow._gcsfs" fi if [ "${PYARROW_TEST_S3}" == "ON" ]; then - python -c "import pyarrow._s3fs" + # S3 ships in the separate pyarrow-s3 wheel; Load via pyarrow.fs. + python -c "import pyarrow.fs; pyarrow.fs.S3FileSystem" fi if [ "${PYARROW_TEST_FLIGHT}" == "ON" ]; then python -c "import pyarrow.flight" diff --git a/ci/scripts/python_wheel_validate_contents.py b/ci/scripts/python_wheel_validate_contents.py index f11b24c11e0a..9b142b81e885 100644 --- a/ci/scripts/python_wheel_validate_contents.py +++ b/ci/scripts/python_wheel_validate_contents.py @@ -40,7 +40,8 @@ def _count_docstrings(source): # TODO(GH-48970): Check stubs ARE present once annotations are complete def validate_wheel(path): p = Path(path) - wheels = list(p.glob('*.whl')) + # Only the core pyarrow wheel; pyarrow_s3-*.whl sits alongside it. + wheels = list(p.glob('pyarrow-*.whl')) error_msg = f"{len(wheels)} wheels found but only 1 expected ({wheels})" assert len(wheels) == 1, error_msg with zipfile.ZipFile(wheels[0]) as wheel_zip: diff --git a/ci/scripts/python_wheel_xlinux_build.sh b/ci/scripts/python_wheel_xlinux_build.sh index a64ca50b61e8..46d36d47878b 100755 --- a/ci/scripts/python_wheel_xlinux_build.sh +++ b/ci/scripts/python_wheel_xlinux_build.sh @@ -225,6 +225,13 @@ popd rm -rf dist/temp-fix-wheel +echo "=== (${PYTHON_VERSION}) Building pyarrow-s3 wheel ===" +# CMake pulls libarrow_s3 from /tmp/arrow-dist via CMAKE_PREFIX_PATH. +python -m build --wheel --no-isolation --outdir dist pyarrow-s3 + echo "=== (${PYTHON_VERSION}) Tag the wheel with ${LINUX_WHEEL_KIND}${LINUX_WHEEL_VERSION} ===" -auditwheel repair dist/pyarrow-*.whl -w repaired_wheels +# libarrow ships in pyarrow and libarrow_s3 in pyarrow-s3: neither wheel +# may graft the other's library. +auditwheel repair --exclude 'libarrow_s3.so.*' dist/pyarrow-*.whl -w repaired_wheels +auditwheel repair --exclude 'libarrow.so.*' dist/pyarrow_s3-*.whl -w repaired_wheels popd diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index f38abce98a41..3db2753cafa0 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -771,12 +771,8 @@ if(PYARROW_BUILD_S3) endif() find_package(ArrowS3 REQUIRED) if(ARROW_BUILD_SHARED) - if(PYARROW_BUNDLE_ARROW_CPP) - bundle_arrow_lib(${ARROW_S3_SHARED_LIB} SO_VERSION ${ARROW_SO_VERSION}) - if(MSVC) - bundle_arrow_import_lib(${ARROW_S3_IMPORT_LIB}) - endif() - endif() + # We do not bundle libarrow_s3 as part of the main PyArrow wheel + # It ships in the separate pyarrow-s3 wheel (python/pyarrow-s3). set(S3_LINK_LIBS ArrowS3::arrow_s3_shared) else() set(S3_LINK_LIBS ArrowS3::arrow_s3_static) diff --git a/python/pyarrow-s3/CMakeLists.txt b/python/pyarrow-s3/CMakeLists.txt new file mode 100644 index 000000000000..3986f59f46ef --- /dev/null +++ b/python/pyarrow-s3/CMakeLists.txt @@ -0,0 +1,31 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +cmake_minimum_required(VERSION 3.25) + +# Nothing is compiled, but Arrow's CMake package expects a language enabled. +project(pyarrow_s3 LANGUAGES CXX) + +find_package(ArrowS3 REQUIRED) + +# Ship only the SONAME file, named the same way bundle_arrow_lib names it in +# python/CMakeLists.txt, so pyarrow's _s3fs resolves it by SONAME. +get_filename_component(ARROW_S3_LIB_REAL ${ARROW_S3_SHARED_LIB} REALPATH) +get_filename_component(ARROW_S3_LIB_NAME ${ARROW_S3_SHARED_LIB} NAME_WE) +install(FILES ${ARROW_S3_LIB_REAL} + DESTINATION pyarrow_s3 + RENAME ${ARROW_S3_LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}.${ARROW_SO_VERSION}) diff --git a/python/pyarrow-s3/pyarrow_s3/__init__.py b/python/pyarrow-s3/pyarrow_s3/__init__.py new file mode 100644 index 000000000000..930f2e2cbac6 --- /dev/null +++ b/python/pyarrow-s3/pyarrow_s3/__init__.py @@ -0,0 +1,31 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import ctypes +import glob +import os + +# libarrow_s3 depends on libarrow, which ships in the pyarrow wheel. +# Importing pyarrow first loads libarrow into the process, so the dynamic +# loader resolves libarrow_s3's dependency on it by SONAME. +import pyarrow # noqa: F401 + +# Keep a reference so the library stays loaded for the process lifetime. +_libarrow_s3 = ctypes.CDLL( + glob.glob(os.path.join(os.path.dirname(__file__), "libarrow_s3.so.*"))[0], + mode=ctypes.RTLD_GLOBAL, +) diff --git a/python/pyarrow-s3/pyproject.toml b/python/pyarrow-s3/pyproject.toml new file mode 100644 index 000000000000..2f298acf0814 --- /dev/null +++ b/python/pyarrow-s3/pyproject.toml @@ -0,0 +1,47 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +[build-system] +requires = ["scikit-build-core>=1.0", "setuptools_scm[toml]>=8"] +build-backend = "scikit_build_core.build" + +[project] +name = "pyarrow-s3" +dynamic = ["version", "dependencies"] +requires-python = ">=3.11" +description = "S3 filesystem support for PyArrow" +license = "Apache-2.0" +# TODO: Add the license files + +[tool.scikit-build] +metadata.version.provider = "scikit_build_core.metadata.setuptools_scm" +wheel.packages = ["pyarrow_s3"] +# The wheel ships a shared library but no CPython extension: one wheel per +# platform (py3-none-) installed to platlib, next to pyarrow. +wheel.py-api = "py3" +wheel.platlib = true + +# Pin pyarrow to exactly this package's version. +[tool.scikit-build.metadata.dependencies] +provider = "scikit_build_core.metadata.template" +result = ["pyarrow=={project[version]}"] + +[tool.setuptools_scm] +root = '../..' +version_scheme = 'guess-next-dev' +git_describe_command = 'git describe --dirty --tags --long --match "apache-arrow-[0-9]*.*"' +fallback_version = '26.0.0a0' diff --git a/python/pyarrow/fs.py b/python/pyarrow/fs.py index 670ccaaf2455..7c8fb1f63cf3 100644 --- a/python/pyarrow/fs.py +++ b/python/pyarrow/fs.py @@ -39,6 +39,7 @@ FileStats = FileInfo _not_imported = [] +_not_imported_reasons = {} try: from pyarrow._azurefs import AzureFileSystem # noqa except ImportError: @@ -54,13 +55,26 @@ except ImportError: _not_imported.append("GcsFileSystem") +try: + # S3 support can be installed separately, as the pyarrow-s3 package. + # Importing it loads libarrow_s3 so that pyarrow._s3fs can be imported. + import pyarrow_s3 # noqa: F401 +except ModuleNotFoundError: + pass + try: from pyarrow._s3fs import ( # noqa AwsDefaultS3RetryStrategy, AwsStandardS3RetryStrategy, S3FileSystem, S3LogLevel, S3RetryStrategy, ensure_s3_initialized, finalize_s3, ensure_s3_finalized, initialize_s3, resolve_s3_region) -except ImportError: +except ImportError as exc: _not_imported.append("S3FileSystem") + if not isinstance(exc, ModuleNotFoundError): + # pyarrow._s3fs exists, but libarrow_s3 could not be loaded. + _not_imported_reasons["S3FileSystem"] = ( + f"{exc}. If pyarrow was installed from PyPI, S3 support is " + "provided by the separate 'pyarrow-s3' package" + ) else: # GH-38364: we don't initialize S3 eagerly as that could lead # to crashes at shutdown even when S3 isn't used. @@ -72,10 +86,10 @@ def __getattr__(name): if name in _not_imported: - raise ImportError( - "The pyarrow installation is not built with support for " - f"'{name}'" - ) + msg = f"The pyarrow installation is not built with support for '{name}'" + if name in _not_imported_reasons: + msg += f" ({_not_imported_reasons[name]})" + raise ImportError(msg) raise AttributeError( f"module 'pyarrow.fs' has no attribute '{name}'" From 5e57c56b501ef1b56ce6038829657d8648343071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Tue, 22 Sep 2026 15:19:35 +0200 Subject: [PATCH 02/13] Allow PyArrow to have optional dependencies --- python/pyproject.toml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/python/pyproject.toml b/python/pyproject.toml index 1556354f6bd7..9e28bb1677a4 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -29,7 +29,7 @@ build-backend = "scikit_build_core.build" [project] name = "pyarrow" -dynamic = ["version"] +dynamic = ["version", "optional-dependencies"] requires-python = ">=3.11" description = "Python library for Apache Arrow" readme = {file = "README.md", content-type = "text/markdown"} @@ -104,6 +104,11 @@ PYARROW_GENERATE_COVERAGE = {env = "PYARROW_GENERATE_COVERAGE", default = "OFF"} PYARROW_CXXFLAGS = {env = "PYARROW_CXXFLAGS", default = ""} PYARROW_REQUIRE_STUB_DOCSTRINGS = {env = "PYARROW_REQUIRE_STUB_DOCSTRINGS", default = "OFF"} +# Optional components shipped as separate wheels, pinned to this exact version. +[tool.scikit-build.metadata.optional-dependencies] +provider = "scikit_build_core.metadata.template" +result = { s3 = ["pyarrow-s3=={project[version]}"] } + [tool.setuptools_scm] root = '..' version_file = 'pyarrow/_generated_version.py' From 33ea0728e3f0285bd2f614d0aabb880ec31ad993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Tue, 22 Sep 2026 15:59:10 +0200 Subject: [PATCH 03/13] Cover from_uri case when pyarrow_s3 not present --- ci/scripts/python_wheel_unix_test.sh | 27 +++++++++++++++++++++++-- ci/scripts/python_wheel_xlinux_build.sh | 12 +++++++---- python/pyarrow/_fs.pyx | 18 +++++++++++++++-- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/ci/scripts/python_wheel_unix_test.sh b/ci/scripts/python_wheel_unix_test.sh index 04d0400ee033..fa4c0289f15c 100755 --- a/ci/scripts/python_wheel_unix_test.sh +++ b/ci/scripts/python_wheel_unix_test.sh @@ -58,8 +58,31 @@ export ARROW_TEST_DATA=${source_dir}/testing/data export PARQUET_TEST_DATA=${source_dir}/cpp/submodules/parquet-testing/data if [ "${INSTALL_PYARROW}" == "ON" ]; then - # Install the built wheels - python -m pip install "${source_dir}"/python/repaired_wheels/*.whl + # TODO: We probably want to cover this case in isolation on it's own internal unit test. + # Install the core wheel alone first: S3 ships in the separate pyarrow-s3 + # wheel, so check that S3 fails with an actionable message without it. + python -m pip install "${source_dir}"/python/repaired_wheels/pyarrow-*.whl + if [ "${ARROW_S3}" == "ON" ]; then + python -c " +import pyarrow.fs as fs +try: + fs.S3FileSystem +except ImportError as e: + assert 'pyarrow-s3' in str(e), e +else: + raise AssertionError('S3FileSystem available without pyarrow-s3') +try: + fs.FileSystem.from_uri('s3://bucket/key?region=us-east-1') +except ValueError as e: + assert 'pyarrow-s3' in str(e), e +else: + raise AssertionError('from_uri resolved s3:// without pyarrow-s3') +" + python -m pip install "${source_dir}"/python/repaired_wheels/pyarrow_s3-*.whl + # With pyarrow-s3 installed, from_uri must work even if pyarrow.fs was + # never imported (it loads pyarrow-s3 on demand). + python -c "from pyarrow._fs import FileSystem; FileSystem.from_uri('s3://bucket/key?region=us-east-1')" + fi fi if [ "${CHECK_IMPORTS}" == "ON" ]; then diff --git a/ci/scripts/python_wheel_xlinux_build.sh b/ci/scripts/python_wheel_xlinux_build.sh index 46d36d47878b..905aa5132599 100755 --- a/ci/scripts/python_wheel_xlinux_build.sh +++ b/ci/scripts/python_wheel_xlinux_build.sh @@ -225,13 +225,17 @@ popd rm -rf dist/temp-fix-wheel -echo "=== (${PYTHON_VERSION}) Building pyarrow-s3 wheel ===" -# CMake pulls libarrow_s3 from /tmp/arrow-dist via CMAKE_PREFIX_PATH. -python -m build --wheel --no-isolation --outdir dist pyarrow-s3 +if [ "${ARROW_S3}" == "ON" ]; then + echo "=== (${PYTHON_VERSION}) Building pyarrow-s3 wheel ===" + # CMake pulls libarrow_s3 from /tmp/arrow-dist via CMAKE_PREFIX_PATH. + python -m build --wheel --no-isolation --outdir dist pyarrow-s3 +fi echo "=== (${PYTHON_VERSION}) Tag the wheel with ${LINUX_WHEEL_KIND}${LINUX_WHEEL_VERSION} ===" # libarrow ships in pyarrow and libarrow_s3 in pyarrow-s3: neither wheel # may graft the other's library. auditwheel repair --exclude 'libarrow_s3.so.*' dist/pyarrow-*.whl -w repaired_wheels -auditwheel repair --exclude 'libarrow.so.*' dist/pyarrow_s3-*.whl -w repaired_wheels +if [ "${ARROW_S3}" == "ON" ]; then + auditwheel repair --exclude 'libarrow.so.*' dist/pyarrow_s3-*.whl -w repaired_wheels +fi popd diff --git a/python/pyarrow/_fs.pyx b/python/pyarrow/_fs.pyx index 10b6ac52dd55..18ef965c5eed 100644 --- a/python/pyarrow/_fs.pyx +++ b/python/pyarrow/_fs.pyx @@ -21,7 +21,7 @@ from cpython.datetime cimport datetime, PyDateTime_DateTime from pyarrow.includes.common cimport * from pyarrow.includes.libarrow_python cimport PyDateTime_to_TimePoint -from pyarrow.lib import _detect_compression, frombytes, tobytes +from pyarrow.lib import ArrowInvalid, _detect_compression, frombytes, tobytes from pyarrow.lib cimport * from pyarrow.util import _stringify_path @@ -499,8 +499,22 @@ cdef class FileSystem(_Weakrefable): """ if isinstance(uri, str) and uri.startswith(("fsspec+", "hf://")): return FileSystem._fsspec_from_uri(uri) - else: + try: return FileSystem._native_from_uri(uri) + except ArrowInvalid as exc: + if not (isinstance(uri, str) and uri.startswith("s3://") + and "Unrecognized filesystem type" in str(exc)): + raise + # S3 support can ship separately, as the pyarrow-s3 package. + # Importing pyarrow.fs loads it if installed, which registers + # the s3 scheme. + import pyarrow.fs + if "S3FileSystem" not in pyarrow.fs._not_imported: + return FileSystem._native_from_uri(uri) + reason = pyarrow.fs._not_imported_reasons.get("S3FileSystem") + if reason is None: + raise + raise ArrowInvalid(f"{exc} ({reason})") from None cdef init(self, const shared_ptr[CFileSystem]& wrapped): self.wrapped = wrapped From 10d0aefcda427ab53747a53e828bb920f7912cd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Tue, 22 Sep 2026 16:44:49 +0200 Subject: [PATCH 04/13] Several macOS fixes --- ci/scripts/python_wheel_macos_build.sh | 8 ++++++++ python/pyarrow-s3/CMakeLists.txt | 14 +++++++++++--- python/pyarrow-s3/pyarrow_s3/__init__.py | 8 +++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/ci/scripts/python_wheel_macos_build.sh b/ci/scripts/python_wheel_macos_build.sh index 4724f8e15246..d71df665c93e 100755 --- a/ci/scripts/python_wheel_macos_build.sh +++ b/ci/scripts/python_wheel_macos_build.sh @@ -193,3 +193,11 @@ fi # Move the verified wheels mkdir -p "${source_dir}/python/repaired_wheels" mv "${source_dir}"/python/dist/*.whl "${source_dir}"/python/repaired_wheels/ + +if [ "${ARROW_S3}" == "ON" ]; then + echo "=== (${PYTHON_VERSION}) Building pyarrow-s3 wheel ===" + # CMake pulls libarrow_s3 from the Arrow install via CMAKE_PREFIX_PATH. + python -m build --wheel --no-isolation \ + --outdir "${source_dir}/python/repaired_wheels" \ + "${source_dir}/python/pyarrow-s3" +fi diff --git a/python/pyarrow-s3/CMakeLists.txt b/python/pyarrow-s3/CMakeLists.txt index 3986f59f46ef..aaea4ddfb45d 100644 --- a/python/pyarrow-s3/CMakeLists.txt +++ b/python/pyarrow-s3/CMakeLists.txt @@ -22,10 +22,18 @@ project(pyarrow_s3 LANGUAGES CXX) find_package(ArrowS3 REQUIRED) -# Ship only the SONAME file, named the same way bundle_arrow_lib names it in -# python/CMakeLists.txt, so pyarrow's _s3fs resolves it by SONAME. +# Ship only the versioned library, named the same way bundle_arrow_lib names +# it in python/CMakeLists.txt, so pyarrow's _s3fs resolves it by its SONAME +# (Linux) or install name (macOS). get_filename_component(ARROW_S3_LIB_REAL ${ARROW_S3_SHARED_LIB} REALPATH) get_filename_component(ARROW_S3_LIB_NAME ${ARROW_S3_SHARED_LIB} NAME_WE) +if(APPLE) + set(ARROW_S3_LIB_INSTALLED_NAME + ${ARROW_S3_LIB_NAME}.${ARROW_SO_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}) +else() + set(ARROW_S3_LIB_INSTALLED_NAME + ${ARROW_S3_LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}.${ARROW_SO_VERSION}) +endif() install(FILES ${ARROW_S3_LIB_REAL} DESTINATION pyarrow_s3 - RENAME ${ARROW_S3_LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}.${ARROW_SO_VERSION}) + RENAME ${ARROW_S3_LIB_INSTALLED_NAME}) diff --git a/python/pyarrow-s3/pyarrow_s3/__init__.py b/python/pyarrow-s3/pyarrow_s3/__init__.py index 930f2e2cbac6..fc1c3cb28df7 100644 --- a/python/pyarrow-s3/pyarrow_s3/__init__.py +++ b/python/pyarrow-s3/pyarrow_s3/__init__.py @@ -18,14 +18,20 @@ import ctypes import glob import os +import sys # libarrow_s3 depends on libarrow, which ships in the pyarrow wheel. # Importing pyarrow first loads libarrow into the process, so the dynamic # loader resolves libarrow_s3's dependency on it by SONAME. import pyarrow # noqa: F401 +if sys.platform == "darwin": + _pattern = "libarrow_s3.*.dylib" +else: + _pattern = "libarrow_s3.so.*" + # Keep a reference so the library stays loaded for the process lifetime. _libarrow_s3 = ctypes.CDLL( - glob.glob(os.path.join(os.path.dirname(__file__), "libarrow_s3.so.*"))[0], + glob.glob(os.path.join(os.path.dirname(__file__), _pattern))[0], mode=ctypes.RTLD_GLOBAL, ) From ef073dfc8ad9d6ce79eecd4c58ee77bd67709498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Tue, 22 Sep 2026 19:51:15 +0200 Subject: [PATCH 05/13] Some Windows fixes --- ci/scripts/python_wheel_windows_build.bat | 19 ++++++++++++++++++- ci/scripts/python_wheel_windows_test.bat | 9 ++++++++- python/pyarrow-s3/CMakeLists.txt | 11 ++++++++++- python/pyarrow-s3/pyarrow_s3/__init__.py | 4 +++- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/ci/scripts/python_wheel_windows_build.bat b/ci/scripts/python_wheel_windows_build.bat index 3805f750d450..7889ec997f89 100644 --- a/ci/scripts/python_wheel_windows_build.bat +++ b/ci/scripts/python_wheel_windows_build.bat @@ -151,8 +151,25 @@ pushd C:\arrow\python for /f %%i in ('dir dist\pyarrow-*.whl /B') do (set WHEEL_NAME=%cd%\dist\%%i) || exit /B 1 echo "Wheel name: %WHEEL_NAME%" +@REM arrow_s3.dll ships in the separate pyarrow-s3 wheel. %PYTHON_CMD% -m delvewheel repair -vv ^ - --ignore-existing --with-mangle ^ + --ignore-existing --with-mangle --exclude arrow_s3.dll ^ -w repaired_wheels %WHEEL_NAME% || exit /B 1 +if not "%ARROW_S3%"=="ON" goto :skip_pyarrow_s3 + +@REM Build pyarrow-s3: CMake pulls arrow_s3.dll from C:\arrow-dist via +@REM CMAKE_PREFIX_PATH. +%PYTHON_CMD% -m build --wheel --no-isolation --outdir dist-s3 pyarrow-s3 || exit /B 1 + +for /f %%i in ('dir dist-s3\pyarrow_s3-*.whl /B') do (set S3_WHEEL_NAME=%cd%\dist-s3\%%i) || exit /B 1 +echo "pyarrow-s3 wheel name: %S3_WHEEL_NAME%" + +@REM arrow.dll ships in the pyarrow wheel. +%PYTHON_CMD% -m delvewheel repair -vv ^ + --ignore-existing --with-mangle --exclude arrow.dll ^ + -w repaired_wheels %S3_WHEEL_NAME% || exit /B 1 + +:skip_pyarrow_s3 + popd diff --git a/ci/scripts/python_wheel_windows_test.bat b/ci/scripts/python_wheel_windows_test.bat index 1e9cacac8bfa..b9f7c7009faa 100755 --- a/ci/scripts/python_wheel_windows_test.bat +++ b/ci/scripts/python_wheel_windows_test.bat @@ -42,12 +42,19 @@ py -0p @REM Install the built wheels %PYTHON_CMD% -m pip install --no-index --find-links=C:\arrow\python\repaired_wheels pyarrow || exit /B 1 +@REM S3 ships in the separate pyarrow-s3 wheel: with only pyarrow installed, +@REM check that S3 fails with an actionable message, then install it. +if "%PYARROW_TEST_S3%"=="ON" %PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_s3_split.py || exit /B 1 +if "%PYARROW_TEST_S3%"=="ON" %PYTHON_CMD% -m pip install --no-index --find-links=C:\arrow\python\repaired_wheels pyarrow-s3 || exit /B 1 +@REM from_uri must work even if pyarrow.fs was never imported. +if "%PYARROW_TEST_S3%"=="ON" %PYTHON_CMD% -c "from pyarrow._fs import FileSystem; FileSystem.from_uri('s3://bucket/key?region=us-east-1')" || exit /B 1 + @REM Test that the modules are importable %PYTHON_CMD% -c "import pyarrow" || exit /B 1 %PYTHON_CMD% -c "import pyarrow._azurefs" || exit /B 1 %PYTHON_CMD% -c "import pyarrow._gcsfs" || exit /B 1 %PYTHON_CMD% -c "import pyarrow._hdfs" || exit /B 1 -%PYTHON_CMD% -c "import pyarrow._s3fs" || exit /B 1 +%PYTHON_CMD% -c "import pyarrow.fs; pyarrow.fs.S3FileSystem" || exit /B 1 %PYTHON_CMD% -c "import pyarrow.csv" || exit /B 1 %PYTHON_CMD% -c "import pyarrow.dataset" || exit /B 1 %PYTHON_CMD% -c "import pyarrow.flight" || exit /B 1 diff --git a/python/pyarrow-s3/CMakeLists.txt b/python/pyarrow-s3/CMakeLists.txt index aaea4ddfb45d..72af49c3c807 100644 --- a/python/pyarrow-s3/CMakeLists.txt +++ b/python/pyarrow-s3/CMakeLists.txt @@ -27,7 +27,9 @@ find_package(ArrowS3 REQUIRED) # (Linux) or install name (macOS). get_filename_component(ARROW_S3_LIB_REAL ${ARROW_S3_SHARED_LIB} REALPATH) get_filename_component(ARROW_S3_LIB_NAME ${ARROW_S3_SHARED_LIB} NAME_WE) -if(APPLE) +if(MSVC) + set(ARROW_S3_LIB_INSTALLED_NAME ${ARROW_S3_LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}) +elseif(APPLE) set(ARROW_S3_LIB_INSTALLED_NAME ${ARROW_S3_LIB_NAME}.${ARROW_SO_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}) else() @@ -37,3 +39,10 @@ endif() install(FILES ${ARROW_S3_LIB_REAL} DESTINATION pyarrow_s3 RENAME ${ARROW_S3_LIB_INSTALLED_NAME}) +if(MSVC) + # The import library, previously bundled in the pyarrow wheel, for C++ + # extensions that link against Arrow S3. + install(FILES ${ARROW_S3_IMPORT_LIB} + DESTINATION pyarrow_s3 + RENAME ${ARROW_S3_LIB_NAME}.lib) +endif() diff --git a/python/pyarrow-s3/pyarrow_s3/__init__.py b/python/pyarrow-s3/pyarrow_s3/__init__.py index fc1c3cb28df7..5078e79ee8bf 100644 --- a/python/pyarrow-s3/pyarrow_s3/__init__.py +++ b/python/pyarrow-s3/pyarrow_s3/__init__.py @@ -25,7 +25,9 @@ # loader resolves libarrow_s3's dependency on it by SONAME. import pyarrow # noqa: F401 -if sys.platform == "darwin": +if sys.platform == "win32": + _pattern = "arrow_s3.dll" +elif sys.platform == "darwin": _pattern = "libarrow_s3.*.dylib" else: _pattern = "libarrow_s3.so.*" From e185ad33ad08f8b744bc9ad41bcb153523d94dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Wed, 23 Sep 2026 11:53:51 +0200 Subject: [PATCH 06/13] Add missing local file to commit --- ci/scripts/python_wheel_check_s3_split.py | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 ci/scripts/python_wheel_check_s3_split.py diff --git a/ci/scripts/python_wheel_check_s3_split.py b/ci/scripts/python_wheel_check_s3_split.py new file mode 100644 index 000000000000..ad4248cd86b5 --- /dev/null +++ b/ci/scripts/python_wheel_check_s3_split.py @@ -0,0 +1,35 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Run with pyarrow installed but not pyarrow-s3: S3 use must fail with a +# message pointing at the pyarrow-s3 package. + +import pyarrow.fs as fs + +try: + fs.S3FileSystem +except ImportError as e: + assert "pyarrow-s3" in str(e), e +else: + raise AssertionError("S3FileSystem available without pyarrow-s3") + +try: + fs.FileSystem.from_uri("s3://bucket/key?region=us-east-1") +except ValueError as e: + assert "pyarrow-s3" in str(e), e +else: + raise AssertionError("from_uri resolved s3:// without pyarrow-s3") From f1b9924e5d67024acb253ef7bd3396b96f5f8ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Wed, 23 Sep 2026 11:58:15 +0200 Subject: [PATCH 07/13] Add missing analyze-existing argument to delvewheel --- ci/scripts/python_wheel_windows_build.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/scripts/python_wheel_windows_build.bat b/ci/scripts/python_wheel_windows_build.bat index 7889ec997f89..13925fe5ef4f 100644 --- a/ci/scripts/python_wheel_windows_build.bat +++ b/ci/scripts/python_wheel_windows_build.bat @@ -167,7 +167,7 @@ echo "pyarrow-s3 wheel name: %S3_WHEEL_NAME%" @REM arrow.dll ships in the pyarrow wheel. %PYTHON_CMD% -m delvewheel repair -vv ^ - --ignore-existing --with-mangle --exclude arrow.dll ^ + --ignore-existing --analyze-existing --with-mangle --exclude arrow.dll ^ -w repaired_wheels %S3_WHEEL_NAME% || exit /B 1 :skip_pyarrow_s3 From e696710734cb27af403c38e814f9421eec877110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Wed, 23 Sep 2026 13:25:20 +0200 Subject: [PATCH 08/13] Fix musllinux by adding INSTALL_RPATH /../pyarrow_s3 to _s3fs --- python/CMakeLists.txt | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 3db2753cafa0..1a3a548a7ae1 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -121,10 +121,11 @@ if(UNIX AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten") # loaded properly if(APPLE) set(CMAKE_INSTALL_NAME_DIR "@rpath") - set(CMAKE_INSTALL_RPATH "@loader_path/") + set(PYARROW_RPATH_ORIGIN "@loader_path") else() - set(CMAKE_INSTALL_RPATH "\$ORIGIN") + set(PYARROW_RPATH_ORIGIN "\$ORIGIN") endif() + set(CMAKE_INSTALL_RPATH "${PYARROW_RPATH_ORIGIN}/") endif() find_program(CCACHE_FOUND ccache) @@ -1083,6 +1084,15 @@ endif() if(PYARROW_BUILD_S3) target_link_libraries(_s3fs PRIVATE ${S3_LINK_LIBS}) + if(PYARROW_BUNDLE_ARROW_CPP AND UNIX) + # libarrow_s3 ships in the sibling pyarrow-s3 wheel, so it is not in any + # directory this extension already searches. Preloading it from + # pyarrow_s3/__init__.py is not enough: musl's loader does not match a + # DT_NEEDED entry against a library that was opened by an explicit path. + set_property(TARGET _s3fs + APPEND + PROPERTY INSTALL_RPATH "${PYARROW_RPATH_ORIGIN}/../pyarrow_s3") + endif() endif() # From 548621bc38cd4ae4dcb3551dc607f3b7cc6fbda5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Fri, 4 Dec 2026 09:28:56 +0100 Subject: [PATCH 09/13] Fix mypy linter --- python/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/pyproject.toml b/python/pyproject.toml index 9e28bb1677a4..50101d4668fd 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -133,7 +133,7 @@ follow_imports = "skip" # Optional dependencies without type hints. [[tool.mypy.overrides]] -module = ["fsspec", "sparse"] +module = ["fsspec", "pyarrow_s3", "sparse"] ignore_missing_imports = true [tool.pyright] From 72bf9b484b106f8a3061b5df7db1d77c142ebbde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Fri, 4 Dec 2026 10:52:28 +0100 Subject: [PATCH 10/13] Move from s3 to cloudfs and add GCS and Azure to pyarrow-cloudfs --- .../python_wheel_check_cloudfs_split.py | 73 +++++++++++++++++++ ci/scripts/python_wheel_check_s3_split.py | 35 --------- ci/scripts/python_wheel_macos_build.sh | 12 ++- ci/scripts/python_wheel_unix_test.sh | 38 ++++------ ci/scripts/python_wheel_validate_contents.py | 2 +- ci/scripts/python_wheel_windows_build.bat | 30 +++++--- ci/scripts/python_wheel_windows_test.bat | 16 ++-- ci/scripts/python_wheel_xlinux_build.sh | 32 +++++--- python/CMakeLists.txt | 44 ++++++----- python/pyarrow-cloudfs/CMakeLists.txt | 66 +++++++++++++++++ .../pyarrow_cloudfs/__init__.py | 55 ++++++++++++++ .../pyproject.toml | 6 +- python/pyarrow-s3/CMakeLists.txt | 48 ------------ python/pyarrow-s3/pyarrow_s3/__init__.py | 39 ---------- python/pyarrow/_fs.pyx | 31 ++++++-- python/pyarrow/fs.py | 47 +++++++----- python/pyproject.toml | 8 +- 17 files changed, 349 insertions(+), 233 deletions(-) create mode 100644 ci/scripts/python_wheel_check_cloudfs_split.py delete mode 100644 ci/scripts/python_wheel_check_s3_split.py create mode 100644 python/pyarrow-cloudfs/CMakeLists.txt create mode 100644 python/pyarrow-cloudfs/pyarrow_cloudfs/__init__.py rename python/{pyarrow-s3 => pyarrow-cloudfs}/pyproject.toml (93%) delete mode 100644 python/pyarrow-s3/CMakeLists.txt delete mode 100644 python/pyarrow-s3/pyarrow_s3/__init__.py diff --git a/ci/scripts/python_wheel_check_cloudfs_split.py b/ci/scripts/python_wheel_check_cloudfs_split.py new file mode 100644 index 000000000000..90614785181e --- /dev/null +++ b/ci/scripts/python_wheel_check_cloudfs_split.py @@ -0,0 +1,73 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Check S3, GCS and Azure against the pyarrow-cloudfs split. +# +# Usage: python_wheel_check_cloudfs_split.py missing|installed +# +# missing: run with pyarrow installed but not pyarrow-cloudfs. Each enabled +# filesystem must fail with a message pointing at pyarrow-cloudfs, both +# from pyarrow.fs and from FileSystem.from_uri. +# installed: run with both installed. FileSystem.from_uri must resolve each +# enabled filesystem even if pyarrow.fs was never imported. +# +# A filesystem is checked when its PYARROW_TEST_ variable is ON. + +import os +import sys + +from pyarrow.lib import ArrowInvalid + +# (PYARROW_TEST_ variable, pyarrow.fs name, URI, FileSystem.type_name) +FILESYSTEMS = [ + ("PYARROW_TEST_S3", "S3FileSystem", + "s3://bucket/key?region=us-east-1", "s3"), + ("PYARROW_TEST_GCS", "GcsFileSystem", + "gs://anonymous@bucket/key", "gcs"), + ("PYARROW_TEST_AZURE", "AzureFileSystem", + "abfs://container@account.dfs.core.windows.net/key" + "?credential_kind=anonymous", "abfs"), +] + +mode = sys.argv[1] +enabled = [fs for fs in FILESYSTEMS if os.environ.get(fs[0]) == "ON"] + +for _, name, uri, type_name in enabled: + if mode == "missing": + try: + from pyarrow._fs import FileSystem + FileSystem.from_uri(uri) + except ArrowInvalid as e: + assert "pyarrow-cloudfs" in str(e), e + else: + raise AssertionError(f"from_uri resolved {uri} without pyarrow-cloudfs") + + import pyarrow.fs as fs + try: + getattr(fs, name) + except ImportError as e: + assert "pyarrow-cloudfs" in str(e), e + else: + raise AssertionError(f"{name} available without pyarrow-cloudfs") + elif mode == "installed": + from pyarrow._fs import FileSystem + filesystem, _ = FileSystem.from_uri(uri) + assert filesystem.type_name == type_name, filesystem.type_name + else: + raise ValueError(f"unknown mode: {mode}") + +print(f"Checked {mode}: {[name for _, name, _, _ in enabled]}") diff --git a/ci/scripts/python_wheel_check_s3_split.py b/ci/scripts/python_wheel_check_s3_split.py deleted file mode 100644 index ad4248cd86b5..000000000000 --- a/ci/scripts/python_wheel_check_s3_split.py +++ /dev/null @@ -1,35 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -# Run with pyarrow installed but not pyarrow-s3: S3 use must fail with a -# message pointing at the pyarrow-s3 package. - -import pyarrow.fs as fs - -try: - fs.S3FileSystem -except ImportError as e: - assert "pyarrow-s3" in str(e), e -else: - raise AssertionError("S3FileSystem available without pyarrow-s3") - -try: - fs.FileSystem.from_uri("s3://bucket/key?region=us-east-1") -except ValueError as e: - assert "pyarrow-s3" in str(e), e -else: - raise AssertionError("from_uri resolved s3:// without pyarrow-s3") diff --git a/ci/scripts/python_wheel_macos_build.sh b/ci/scripts/python_wheel_macos_build.sh index d71df665c93e..24e79f254414 100755 --- a/ci/scripts/python_wheel_macos_build.sh +++ b/ci/scripts/python_wheel_macos_build.sh @@ -194,10 +194,14 @@ fi mkdir -p "${source_dir}/python/repaired_wheels" mv "${source_dir}"/python/dist/*.whl "${source_dir}"/python/repaired_wheels/ -if [ "${ARROW_S3}" == "ON" ]; then - echo "=== (${PYTHON_VERSION}) Building pyarrow-s3 wheel ===" - # CMake pulls libarrow_s3 from the Arrow install via CMAKE_PREFIX_PATH. +if [ "${ARROW_S3}" == "ON" ] || [ "${ARROW_GCS}" == "ON" ] || [ "${ARROW_AZURE}" == "ON" ]; then + echo "=== (${PYTHON_VERSION}) Building pyarrow-cloudfs wheel ===" + # CMake pulls libarrow_s3, libarrow_gcs and libarrow_azure from the Arrow + # install via CMAKE_PREFIX_PATH. python -m build --wheel --no-isolation \ --outdir "${source_dir}/python/repaired_wheels" \ - "${source_dir}/python/pyarrow-s3" + -C cmake.define.PYARROW_CLOUDFS_S3="${ARROW_S3}" \ + -C cmake.define.PYARROW_CLOUDFS_GCS="${ARROW_GCS}" \ + -C cmake.define.PYARROW_CLOUDFS_AZURE="${ARROW_AZURE}" \ + "${source_dir}/python/pyarrow-cloudfs" fi diff --git a/ci/scripts/python_wheel_unix_test.sh b/ci/scripts/python_wheel_unix_test.sh index fa4c0289f15c..a2e0ef33c539 100755 --- a/ci/scripts/python_wheel_unix_test.sh +++ b/ci/scripts/python_wheel_unix_test.sh @@ -58,30 +58,14 @@ export ARROW_TEST_DATA=${source_dir}/testing/data export PARQUET_TEST_DATA=${source_dir}/cpp/submodules/parquet-testing/data if [ "${INSTALL_PYARROW}" == "ON" ]; then - # TODO: We probably want to cover this case in isolation on it's own internal unit test. - # Install the core wheel alone first: S3 ships in the separate pyarrow-s3 - # wheel, so check that S3 fails with an actionable message without it. + # Install the core wheel alone first: S3, GCS and Azure ship in the + # separate pyarrow-cloudfs wheel, so check that they fail with an + # actionable message without it. python -m pip install "${source_dir}"/python/repaired_wheels/pyarrow-*.whl - if [ "${ARROW_S3}" == "ON" ]; then - python -c " -import pyarrow.fs as fs -try: - fs.S3FileSystem -except ImportError as e: - assert 'pyarrow-s3' in str(e), e -else: - raise AssertionError('S3FileSystem available without pyarrow-s3') -try: - fs.FileSystem.from_uri('s3://bucket/key?region=us-east-1') -except ValueError as e: - assert 'pyarrow-s3' in str(e), e -else: - raise AssertionError('from_uri resolved s3:// without pyarrow-s3') -" - python -m pip install "${source_dir}"/python/repaired_wheels/pyarrow_s3-*.whl - # With pyarrow-s3 installed, from_uri must work even if pyarrow.fs was - # never imported (it loads pyarrow-s3 on demand). - python -c "from pyarrow._fs import FileSystem; FileSystem.from_uri('s3://bucket/key?region=us-east-1')" + if [ "${PYARROW_TEST_S3}" == "ON" ] || [ "${PYARROW_TEST_GCS}" == "ON" ] || [ "${PYARROW_TEST_AZURE}" == "ON" ]; then + python "${source_dir}/ci/scripts/python_wheel_check_cloudfs_split.py" missing + python -m pip install "${source_dir}"/python/repaired_wheels/pyarrow_cloudfs-*.whl + python "${source_dir}/ci/scripts/python_wheel_check_cloudfs_split.py" installed fi fi @@ -97,11 +81,15 @@ import pyarrow.json import pyarrow.orc import pyarrow.parquet " + # S3, GCS and Azure ship in the separate pyarrow-cloudfs wheel, which + # pyarrow.fs loads, so import them through pyarrow.fs. + if [ "${PYARROW_TEST_AZURE}" == "ON" ]; then + python -c "import pyarrow.fs; pyarrow.fs.AzureFileSystem" + fi if [ "${PYARROW_TEST_GCS}" == "ON" ]; then - python -c "import pyarrow._gcsfs" + python -c "import pyarrow.fs; pyarrow.fs.GcsFileSystem" fi if [ "${PYARROW_TEST_S3}" == "ON" ]; then - # S3 ships in the separate pyarrow-s3 wheel; Load via pyarrow.fs. python -c "import pyarrow.fs; pyarrow.fs.S3FileSystem" fi if [ "${PYARROW_TEST_FLIGHT}" == "ON" ]; then diff --git a/ci/scripts/python_wheel_validate_contents.py b/ci/scripts/python_wheel_validate_contents.py index 9b142b81e885..aa21137b96ce 100644 --- a/ci/scripts/python_wheel_validate_contents.py +++ b/ci/scripts/python_wheel_validate_contents.py @@ -40,7 +40,7 @@ def _count_docstrings(source): # TODO(GH-48970): Check stubs ARE present once annotations are complete def validate_wheel(path): p = Path(path) - # Only the core pyarrow wheel; pyarrow_s3-*.whl sits alongside it. + # Only the core pyarrow wheel; pyarrow_cloudfs-*.whl sits alongside it. wheels = list(p.glob('pyarrow-*.whl')) error_msg = f"{len(wheels)} wheels found but only 1 expected ({wheels})" assert len(wheels) == 1, error_msg diff --git a/ci/scripts/python_wheel_windows_build.bat b/ci/scripts/python_wheel_windows_build.bat index 13925fe5ef4f..6a6761c56792 100644 --- a/ci/scripts/python_wheel_windows_build.bat +++ b/ci/scripts/python_wheel_windows_build.bat @@ -151,25 +151,35 @@ pushd C:\arrow\python for /f %%i in ('dir dist\pyarrow-*.whl /B') do (set WHEEL_NAME=%cd%\dist\%%i) || exit /B 1 echo "Wheel name: %WHEEL_NAME%" -@REM arrow_s3.dll ships in the separate pyarrow-s3 wheel. +@REM arrow_s3.dll, arrow_gcs.dll and arrow_azure.dll ship in the separate +@REM pyarrow-cloudfs wheel. %PYTHON_CMD% -m delvewheel repair -vv ^ - --ignore-existing --with-mangle --exclude arrow_s3.dll ^ + --ignore-existing --with-mangle ^ + --exclude "arrow_s3.dll;arrow_gcs.dll;arrow_azure.dll" ^ -w repaired_wheels %WHEEL_NAME% || exit /B 1 -if not "%ARROW_S3%"=="ON" goto :skip_pyarrow_s3 +set BUILD_CLOUDFS=OFF +if "%ARROW_S3%"=="ON" set BUILD_CLOUDFS=ON +if "%ARROW_GCS%"=="ON" set BUILD_CLOUDFS=ON +if "%ARROW_AZURE%"=="ON" set BUILD_CLOUDFS=ON +if not "%BUILD_CLOUDFS%"=="ON" goto :skip_pyarrow_cloudfs -@REM Build pyarrow-s3: CMake pulls arrow_s3.dll from C:\arrow-dist via -@REM CMAKE_PREFIX_PATH. -%PYTHON_CMD% -m build --wheel --no-isolation --outdir dist-s3 pyarrow-s3 || exit /B 1 +@REM Build pyarrow-cloudfs: CMake pulls arrow_s3.dll, arrow_gcs.dll and +@REM arrow_azure.dll from C:\arrow-dist via CMAKE_PREFIX_PATH. +%PYTHON_CMD% -m build --wheel --no-isolation --outdir dist-cloudfs ^ + -C cmake.define.PYARROW_CLOUDFS_S3=%ARROW_S3% ^ + -C cmake.define.PYARROW_CLOUDFS_GCS=%ARROW_GCS% ^ + -C cmake.define.PYARROW_CLOUDFS_AZURE=%ARROW_AZURE% ^ + pyarrow-cloudfs || exit /B 1 -for /f %%i in ('dir dist-s3\pyarrow_s3-*.whl /B') do (set S3_WHEEL_NAME=%cd%\dist-s3\%%i) || exit /B 1 -echo "pyarrow-s3 wheel name: %S3_WHEEL_NAME%" +for /f %%i in ('dir dist-cloudfs\pyarrow_cloudfs-*.whl /B') do (set CLOUDFS_WHEEL_NAME=%cd%\dist-cloudfs\%%i) || exit /B 1 +echo "pyarrow-cloudfs wheel name: %CLOUDFS_WHEEL_NAME%" @REM arrow.dll ships in the pyarrow wheel. %PYTHON_CMD% -m delvewheel repair -vv ^ --ignore-existing --analyze-existing --with-mangle --exclude arrow.dll ^ - -w repaired_wheels %S3_WHEEL_NAME% || exit /B 1 + -w repaired_wheels %CLOUDFS_WHEEL_NAME% || exit /B 1 -:skip_pyarrow_s3 +:skip_pyarrow_cloudfs popd diff --git a/ci/scripts/python_wheel_windows_test.bat b/ci/scripts/python_wheel_windows_test.bat index b9f7c7009faa..8dd7243feeb1 100755 --- a/ci/scripts/python_wheel_windows_test.bat +++ b/ci/scripts/python_wheel_windows_test.bat @@ -42,17 +42,17 @@ py -0p @REM Install the built wheels %PYTHON_CMD% -m pip install --no-index --find-links=C:\arrow\python\repaired_wheels pyarrow || exit /B 1 -@REM S3 ships in the separate pyarrow-s3 wheel: with only pyarrow installed, -@REM check that S3 fails with an actionable message, then install it. -if "%PYARROW_TEST_S3%"=="ON" %PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_s3_split.py || exit /B 1 -if "%PYARROW_TEST_S3%"=="ON" %PYTHON_CMD% -m pip install --no-index --find-links=C:\arrow\python\repaired_wheels pyarrow-s3 || exit /B 1 -@REM from_uri must work even if pyarrow.fs was never imported. -if "%PYARROW_TEST_S3%"=="ON" %PYTHON_CMD% -c "from pyarrow._fs import FileSystem; FileSystem.from_uri('s3://bucket/key?region=us-east-1')" || exit /B 1 +@REM S3, GCS and Azure ship in the separate pyarrow-cloudfs wheel: with only +@REM pyarrow installed, check that they fail with an actionable message, then +@REM install it and check that they work. +%PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_cloudfs_split.py missing || exit /B 1 +%PYTHON_CMD% -m pip install --no-index --find-links=C:\arrow\python\repaired_wheels pyarrow-cloudfs || exit /B 1 +%PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_cloudfs_split.py installed || exit /B 1 @REM Test that the modules are importable %PYTHON_CMD% -c "import pyarrow" || exit /B 1 -%PYTHON_CMD% -c "import pyarrow._azurefs" || exit /B 1 -%PYTHON_CMD% -c "import pyarrow._gcsfs" || exit /B 1 +%PYTHON_CMD% -c "import pyarrow.fs; pyarrow.fs.AzureFileSystem" || exit /B 1 +%PYTHON_CMD% -c "import pyarrow.fs; pyarrow.fs.GcsFileSystem" || exit /B 1 %PYTHON_CMD% -c "import pyarrow._hdfs" || exit /B 1 %PYTHON_CMD% -c "import pyarrow.fs; pyarrow.fs.S3FileSystem" || exit /B 1 %PYTHON_CMD% -c "import pyarrow.csv" || exit /B 1 diff --git a/ci/scripts/python_wheel_xlinux_build.sh b/ci/scripts/python_wheel_xlinux_build.sh index 905aa5132599..75943778bee8 100755 --- a/ci/scripts/python_wheel_xlinux_build.sh +++ b/ci/scripts/python_wheel_xlinux_build.sh @@ -225,17 +225,31 @@ popd rm -rf dist/temp-fix-wheel -if [ "${ARROW_S3}" == "ON" ]; then - echo "=== (${PYTHON_VERSION}) Building pyarrow-s3 wheel ===" - # CMake pulls libarrow_s3 from /tmp/arrow-dist via CMAKE_PREFIX_PATH. - python -m build --wheel --no-isolation --outdir dist pyarrow-s3 +build_cloudfs=OFF +if [ "${ARROW_S3}" == "ON" ] || [ "${ARROW_GCS}" == "ON" ] || [ "${ARROW_AZURE}" == "ON" ]; then + build_cloudfs=ON +fi + +if [ "${build_cloudfs}" == "ON" ]; then + echo "=== (${PYTHON_VERSION}) Building pyarrow-cloudfs wheel ===" + # CMake pulls libarrow_s3, libarrow_gcs and libarrow_azure from + # /tmp/arrow-dist via CMAKE_PREFIX_PATH. + python -m build --wheel --no-isolation --outdir dist \ + -C cmake.define.PYARROW_CLOUDFS_S3="${ARROW_S3}" \ + -C cmake.define.PYARROW_CLOUDFS_GCS="${ARROW_GCS}" \ + -C cmake.define.PYARROW_CLOUDFS_AZURE="${ARROW_AZURE}" \ + pyarrow-cloudfs fi echo "=== (${PYTHON_VERSION}) Tag the wheel with ${LINUX_WHEEL_KIND}${LINUX_WHEEL_VERSION} ===" -# libarrow ships in pyarrow and libarrow_s3 in pyarrow-s3: neither wheel -# may graft the other's library. -auditwheel repair --exclude 'libarrow_s3.so.*' dist/pyarrow-*.whl -w repaired_wheels -if [ "${ARROW_S3}" == "ON" ]; then - auditwheel repair --exclude 'libarrow.so.*' dist/pyarrow_s3-*.whl -w repaired_wheels +# libarrow ships in pyarrow, and libarrow_s3, libarrow_gcs and libarrow_azure +# in pyarrow-cloudfs: neither wheel may graft the other's libraries. +auditwheel repair \ + --exclude 'libarrow_s3.so.*' \ + --exclude 'libarrow_gcs.so.*' \ + --exclude 'libarrow_azure.so.*' \ + dist/pyarrow-*.whl -w repaired_wheels +if [ "${build_cloudfs}" == "ON" ]; then + auditwheel repair --exclude 'libarrow.so.*' dist/pyarrow_cloudfs-*.whl -w repaired_wheels fi popd diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 1a3a548a7ae1..4d1211a7aa53 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -732,12 +732,8 @@ if(PYARROW_BUILD_AZURE) endif() find_package(ArrowAzure REQUIRED) if(ARROW_BUILD_SHARED) - if(PYARROW_BUNDLE_ARROW_CPP) - bundle_arrow_lib(${ARROW_AZURE_SHARED_LIB} SO_VERSION ${ARROW_SO_VERSION}) - if(MSVC) - bundle_arrow_import_lib(${ARROW_AZURE_IMPORT_LIB}) - endif() - endif() + # Not bundled in the PyArrow wheel: it ships in the separate + # pyarrow-cloudfs wheel (python/pyarrow-cloudfs). set(AZURE_LINK_LIBS ArrowAzure::arrow_azure_shared) else() set(AZURE_LINK_LIBS ArrowAzure::arrow_azure_static) @@ -752,12 +748,8 @@ if(PYARROW_BUILD_GCS) endif() find_package(ArrowGcs REQUIRED) if(ARROW_BUILD_SHARED) - if(PYARROW_BUNDLE_ARROW_CPP) - bundle_arrow_lib(${ARROW_GCS_SHARED_LIB} SO_VERSION ${ARROW_SO_VERSION}) - if(MSVC) - bundle_arrow_import_lib(${ARROW_GCS_IMPORT_LIB}) - endif() - endif() + # Not bundled in the PyArrow wheel: it ships in the separate + # pyarrow-cloudfs wheel (python/pyarrow-cloudfs). set(GCS_LINK_LIBS ArrowGcs::arrow_gcs_shared) else() set(GCS_LINK_LIBS ArrowGcs::arrow_gcs_static) @@ -772,8 +764,8 @@ if(PYARROW_BUILD_S3) endif() find_package(ArrowS3 REQUIRED) if(ARROW_BUILD_SHARED) - # We do not bundle libarrow_s3 as part of the main PyArrow wheel - # It ships in the separate pyarrow-s3 wheel (python/pyarrow-s3). + # Not bundled in the PyArrow wheel: it ships in the separate + # pyarrow-cloudfs wheel (python/pyarrow-cloudfs). set(S3_LINK_LIBS ArrowS3::arrow_s3_shared) else() set(S3_LINK_LIBS ArrowS3::arrow_s3_static) @@ -1084,15 +1076,21 @@ endif() if(PYARROW_BUILD_S3) target_link_libraries(_s3fs PRIVATE ${S3_LINK_LIBS}) - if(PYARROW_BUNDLE_ARROW_CPP AND UNIX) - # libarrow_s3 ships in the sibling pyarrow-s3 wheel, so it is not in any - # directory this extension already searches. Preloading it from - # pyarrow_s3/__init__.py is not enough: musl's loader does not match a - # DT_NEEDED entry against a library that was opened by an explicit path. - set_property(TARGET _s3fs - APPEND - PROPERTY INSTALL_RPATH "${PYARROW_RPATH_ORIGIN}/../pyarrow_s3") - endif() +endif() + +if(PYARROW_BUNDLE_ARROW_CPP AND UNIX) + # libarrow_s3, libarrow_gcs and libarrow_azure ship in the sibling + # pyarrow-cloudfs wheel, so they are not in any directory these extensions + # already search. Preloading them from pyarrow_cloudfs/__init__.py is not + # enough: musl's loader does not match a DT_NEEDED entry against a library + # that was opened by an explicit path. + foreach(cloudfs_extension _azurefs _gcsfs _s3fs) + if(TARGET ${cloudfs_extension}) + set_property(TARGET ${cloudfs_extension} + APPEND + PROPERTY INSTALL_RPATH "${PYARROW_RPATH_ORIGIN}/../pyarrow_cloudfs") + endif() + endforeach() endif() # diff --git a/python/pyarrow-cloudfs/CMakeLists.txt b/python/pyarrow-cloudfs/CMakeLists.txt new file mode 100644 index 000000000000..81469503c86b --- /dev/null +++ b/python/pyarrow-cloudfs/CMakeLists.txt @@ -0,0 +1,66 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +cmake_minimum_required(VERSION 3.25) + +# Nothing is compiled, but Arrow's CMake packages expect a language enabled. +project(pyarrow_cloudfs LANGUAGES CXX) + +# Which libraries to ship. The wheel build scripts pass the same ARROW_S3, +# ARROW_GCS and ARROW_AZURE values they build Arrow C++ with, so this wheel +# ships exactly what the pyarrow wheel leaves out. +option(PYARROW_CLOUDFS_S3 "Ship libarrow_s3" ON) +option(PYARROW_CLOUDFS_GCS "Ship libarrow_gcs" ON) +option(PYARROW_CLOUDFS_AZURE "Ship libarrow_azure" ON) + +# Install one Arrow library under the name pyarrow's extensions look it up by: +# its SONAME on Linux, its install name on macOS, the DLL name on Windows. +# The naming matches bundle_arrow_lib in python/CMakeLists.txt. +function(pyarrow_cloudfs_install_lib shared_lib import_lib) + get_filename_component(lib_real "${shared_lib}" REALPATH) + get_filename_component(lib_name "${shared_lib}" NAME_WE) + if(MSVC) + set(installed_name "${lib_name}${CMAKE_SHARED_LIBRARY_SUFFIX}") + elseif(APPLE) + set(installed_name "${lib_name}.${ARROW_SO_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}") + else() + set(installed_name "${lib_name}${CMAKE_SHARED_LIBRARY_SUFFIX}.${ARROW_SO_VERSION}") + endif() + install(FILES "${lib_real}" + DESTINATION pyarrow_cloudfs + RENAME "${installed_name}") + if(MSVC) + # The import library, previously bundled in the pyarrow wheel, for C++ + # extensions that link against these libraries. + install(FILES "${import_lib}" + DESTINATION pyarrow_cloudfs + RENAME "${lib_name}.lib") + endif() +endfunction() + +if(PYARROW_CLOUDFS_S3) + find_package(ArrowS3 REQUIRED) + pyarrow_cloudfs_install_lib("${ARROW_S3_SHARED_LIB}" "${ARROW_S3_IMPORT_LIB}") +endif() +if(PYARROW_CLOUDFS_GCS) + find_package(ArrowGcs REQUIRED) + pyarrow_cloudfs_install_lib("${ARROW_GCS_SHARED_LIB}" "${ARROW_GCS_IMPORT_LIB}") +endif() +if(PYARROW_CLOUDFS_AZURE) + find_package(ArrowAzure REQUIRED) + pyarrow_cloudfs_install_lib("${ARROW_AZURE_SHARED_LIB}" "${ARROW_AZURE_IMPORT_LIB}") +endif() diff --git a/python/pyarrow-cloudfs/pyarrow_cloudfs/__init__.py b/python/pyarrow-cloudfs/pyarrow_cloudfs/__init__.py new file mode 100644 index 000000000000..8d42bfbf1fc9 --- /dev/null +++ b/python/pyarrow-cloudfs/pyarrow_cloudfs/__init__.py @@ -0,0 +1,55 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +S3, GCS and Azure filesystem support for PyArrow wheels. + +pyarrow imports this package itself, from pyarrow.fs. Importing it directly +is not needed. +""" + +import ctypes +import glob +import os +import sys + +# These libraries depend on libarrow, which ships in the pyarrow wheel. +# Importing pyarrow first loads libarrow into the process, so the dynamic +# loader resolves that dependency by SONAME. +import pyarrow # noqa: F401 + + +def _pattern(name): + if sys.platform == "win32": + return f"{name}.dll" + elif sys.platform == "darwin": + return f"lib{name}.*.dylib" + else: + return f"lib{name}.so.*" + + +_here = os.path.dirname(__file__) + +# Keep references so the libraries stay loaded for the process lifetime. +# A library missing from the wheel is skipped: the build options +# PYARROW_CLOUDFS_S3, PYARROW_CLOUDFS_GCS and PYARROW_CLOUDFS_AZURE decide +# which ones ship. +_libraries = {} +for _name in ("arrow_s3", "arrow_gcs", "arrow_azure"): + _matches = glob.glob(os.path.join(_here, _pattern(_name))) + if _matches: + _libraries[_name] = ctypes.CDLL(_matches[0], mode=ctypes.RTLD_GLOBAL) diff --git a/python/pyarrow-s3/pyproject.toml b/python/pyarrow-cloudfs/pyproject.toml similarity index 93% rename from python/pyarrow-s3/pyproject.toml rename to python/pyarrow-cloudfs/pyproject.toml index 2f298acf0814..1c42520d0147 100644 --- a/python/pyarrow-s3/pyproject.toml +++ b/python/pyarrow-cloudfs/pyproject.toml @@ -20,16 +20,16 @@ requires = ["scikit-build-core>=1.0", "setuptools_scm[toml]>=8"] build-backend = "scikit_build_core.build" [project] -name = "pyarrow-s3" +name = "pyarrow-cloudfs" dynamic = ["version", "dependencies"] requires-python = ">=3.11" -description = "S3 filesystem support for PyArrow" +description = "S3, GCS and Azure filesystem support for PyArrow" license = "Apache-2.0" # TODO: Add the license files [tool.scikit-build] metadata.version.provider = "scikit_build_core.metadata.setuptools_scm" -wheel.packages = ["pyarrow_s3"] +wheel.packages = ["pyarrow_cloudfs"] # The wheel ships a shared library but no CPython extension: one wheel per # platform (py3-none-) installed to platlib, next to pyarrow. wheel.py-api = "py3" diff --git a/python/pyarrow-s3/CMakeLists.txt b/python/pyarrow-s3/CMakeLists.txt deleted file mode 100644 index 72af49c3c807..000000000000 --- a/python/pyarrow-s3/CMakeLists.txt +++ /dev/null @@ -1,48 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -cmake_minimum_required(VERSION 3.25) - -# Nothing is compiled, but Arrow's CMake package expects a language enabled. -project(pyarrow_s3 LANGUAGES CXX) - -find_package(ArrowS3 REQUIRED) - -# Ship only the versioned library, named the same way bundle_arrow_lib names -# it in python/CMakeLists.txt, so pyarrow's _s3fs resolves it by its SONAME -# (Linux) or install name (macOS). -get_filename_component(ARROW_S3_LIB_REAL ${ARROW_S3_SHARED_LIB} REALPATH) -get_filename_component(ARROW_S3_LIB_NAME ${ARROW_S3_SHARED_LIB} NAME_WE) -if(MSVC) - set(ARROW_S3_LIB_INSTALLED_NAME ${ARROW_S3_LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}) -elseif(APPLE) - set(ARROW_S3_LIB_INSTALLED_NAME - ${ARROW_S3_LIB_NAME}.${ARROW_SO_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}) -else() - set(ARROW_S3_LIB_INSTALLED_NAME - ${ARROW_S3_LIB_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}.${ARROW_SO_VERSION}) -endif() -install(FILES ${ARROW_S3_LIB_REAL} - DESTINATION pyarrow_s3 - RENAME ${ARROW_S3_LIB_INSTALLED_NAME}) -if(MSVC) - # The import library, previously bundled in the pyarrow wheel, for C++ - # extensions that link against Arrow S3. - install(FILES ${ARROW_S3_IMPORT_LIB} - DESTINATION pyarrow_s3 - RENAME ${ARROW_S3_LIB_NAME}.lib) -endif() diff --git a/python/pyarrow-s3/pyarrow_s3/__init__.py b/python/pyarrow-s3/pyarrow_s3/__init__.py deleted file mode 100644 index 5078e79ee8bf..000000000000 --- a/python/pyarrow-s3/pyarrow_s3/__init__.py +++ /dev/null @@ -1,39 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -import ctypes -import glob -import os -import sys - -# libarrow_s3 depends on libarrow, which ships in the pyarrow wheel. -# Importing pyarrow first loads libarrow into the process, so the dynamic -# loader resolves libarrow_s3's dependency on it by SONAME. -import pyarrow # noqa: F401 - -if sys.platform == "win32": - _pattern = "arrow_s3.dll" -elif sys.platform == "darwin": - _pattern = "libarrow_s3.*.dylib" -else: - _pattern = "libarrow_s3.so.*" - -# Keep a reference so the library stays loaded for the process lifetime. -_libarrow_s3 = ctypes.CDLL( - glob.glob(os.path.join(os.path.dirname(__file__), _pattern))[0], - mode=ctypes.RTLD_GLOBAL, -) diff --git a/python/pyarrow/_fs.pyx b/python/pyarrow/_fs.pyx index 18ef965c5eed..cdaa01573fc0 100644 --- a/python/pyarrow/_fs.pyx +++ b/python/pyarrow/_fs.pyx @@ -408,6 +408,17 @@ cdef class FileSelector(_Weakrefable): return f"" +# URI schemes whose filesystems ship in the pyarrow-cloudfs package for PyPI +# wheels, mapped to the pyarrow.fs name that records why they are missing. +_CLOUDFS_SCHEMES = { + "s3": "S3FileSystem", + "gs": "GcsFileSystem", + "gcs": "GcsFileSystem", + "abfs": "AzureFileSystem", + "abfss": "AzureFileSystem", +} + + cdef class FileSystem(_Weakrefable): """ Abstract file system API. @@ -502,17 +513,23 @@ cdef class FileSystem(_Weakrefable): try: return FileSystem._native_from_uri(uri) except ArrowInvalid as exc: - if not (isinstance(uri, str) and uri.startswith("s3://") - and "Unrecognized filesystem type" in str(exc)): + if "Unrecognized filesystem type" not in str(exc): + raise + uri_str = frombytes(uri) if isinstance(uri, bytes) else uri + scheme = (uri_str.partition("://")[0].lower() + if isinstance(uri_str, str) else None) + name = _CLOUDFS_SCHEMES.get(scheme) + if name is None: raise - # S3 support can ship separately, as the pyarrow-s3 package. - # Importing pyarrow.fs loads it if installed, which registers - # the s3 scheme. + # S3, GCS and Azure support can ship separately, as the + # pyarrow-cloudfs package. Importing pyarrow.fs loads it if + # installed, which registers the scheme. import pyarrow.fs - if "S3FileSystem" not in pyarrow.fs._not_imported: + if name not in pyarrow.fs._not_imported: return FileSystem._native_from_uri(uri) - reason = pyarrow.fs._not_imported_reasons.get("S3FileSystem") + reason = pyarrow.fs._not_imported_reasons.get(name) if reason is None: + # pyarrow was built without this filesystem. raise raise ArrowInvalid(f"{exc} ({reason})") from None diff --git a/python/pyarrow/fs.py b/python/pyarrow/fs.py index 7c8fb1f63cf3..3c6df8c1741c 100644 --- a/python/pyarrow/fs.py +++ b/python/pyarrow/fs.py @@ -39,11 +39,33 @@ FileStats = FileInfo _not_imported = [] -_not_imported_reasons = {} +_not_imported_reasons: dict[str, str] = {} + +try: + # S3, GCS and Azure support can be installed separately, as the + # pyarrow-cloudfs package. Importing it loads libarrow_s3, libarrow_gcs + # and libarrow_azure so that pyarrow._s3fs, _gcsfs and _azurefs can be + # imported. + import pyarrow_cloudfs # noqa: F401 +except ModuleNotFoundError: + pass + + +def _cloudfs_not_imported(name, extra, exc): + _not_imported.append(name) + if not isinstance(exc, ModuleNotFoundError): + # The extension exists, but its Arrow library could not be loaded. + _not_imported_reasons[name] = ( + f"{exc}. If pyarrow was installed from PyPI, this support is " + "provided by the separate 'pyarrow-cloudfs' package: install it " + f"with 'pip install \"pyarrow[{extra}]\"'" + ) + + try: from pyarrow._azurefs import AzureFileSystem # noqa -except ImportError: - _not_imported.append("AzureFileSystem") +except ImportError as exc: + _cloudfs_not_imported("AzureFileSystem", "azure", exc) try: from pyarrow._hdfs import HadoopFileSystem # noqa @@ -52,15 +74,8 @@ try: from pyarrow._gcsfs import GcsFileSystem # noqa -except ImportError: - _not_imported.append("GcsFileSystem") - -try: - # S3 support can be installed separately, as the pyarrow-s3 package. - # Importing it loads libarrow_s3 so that pyarrow._s3fs can be imported. - import pyarrow_s3 # noqa: F401 -except ModuleNotFoundError: - pass +except ImportError as exc: + _cloudfs_not_imported("GcsFileSystem", "gcs", exc) try: from pyarrow._s3fs import ( # noqa @@ -68,13 +83,7 @@ S3FileSystem, S3LogLevel, S3RetryStrategy, ensure_s3_initialized, finalize_s3, ensure_s3_finalized, initialize_s3, resolve_s3_region) except ImportError as exc: - _not_imported.append("S3FileSystem") - if not isinstance(exc, ModuleNotFoundError): - # pyarrow._s3fs exists, but libarrow_s3 could not be loaded. - _not_imported_reasons["S3FileSystem"] = ( - f"{exc}. If pyarrow was installed from PyPI, S3 support is " - "provided by the separate 'pyarrow-s3' package" - ) + _cloudfs_not_imported("S3FileSystem", "s3", exc) else: # GH-38364: we don't initialize S3 eagerly as that could lead # to crashes at shutdown even when S3 isn't used. diff --git a/python/pyproject.toml b/python/pyproject.toml index 50101d4668fd..3d36e569f531 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -107,7 +107,11 @@ PYARROW_REQUIRE_STUB_DOCSTRINGS = {env = "PYARROW_REQUIRE_STUB_DOCSTRINGS", defa # Optional components shipped as separate wheels, pinned to this exact version. [tool.scikit-build.metadata.optional-dependencies] provider = "scikit_build_core.metadata.template" -result = { s3 = ["pyarrow-s3=={project[version]}"] } + +[tool.scikit-build.metadata.optional-dependencies.result] +s3 = ["pyarrow-cloudfs=={project[version]}"] +gcs = ["pyarrow-cloudfs=={project[version]}"] +azure = ["pyarrow-cloudfs=={project[version]}"] [tool.setuptools_scm] root = '..' @@ -133,7 +137,7 @@ follow_imports = "skip" # Optional dependencies without type hints. [[tool.mypy.overrides]] -module = ["fsspec", "pyarrow_s3", "sparse"] +module = ["fsspec", "pyarrow_cloudfs", "sparse"] ignore_missing_imports = true [tool.pyright] From d62219d3d12994eeec15cb4b97a5b415a7eadf42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Fri, 4 Dec 2026 13:19:17 +0100 Subject: [PATCH 11/13] Add version check --- .../python_wheel_check_cloudfs_split.py | 92 ++++++++++++++++++- ci/scripts/python_wheel_unix_test.sh | 1 + ci/scripts/python_wheel_windows_test.bat | 1 + .../pyarrow_cloudfs/__init__.py | 17 +++- python/pyarrow/fs.py | 71 +++++++++++--- 5 files changed, 168 insertions(+), 14 deletions(-) diff --git a/ci/scripts/python_wheel_check_cloudfs_split.py b/ci/scripts/python_wheel_check_cloudfs_split.py index 90614785181e..09d172a317c7 100644 --- a/ci/scripts/python_wheel_check_cloudfs_split.py +++ b/ci/scripts/python_wheel_check_cloudfs_split.py @@ -17,18 +17,28 @@ # Check S3, GCS and Azure against the pyarrow-cloudfs split. # -# Usage: python_wheel_check_cloudfs_split.py missing|installed +# Usage: python_wheel_check_cloudfs_split.py missing|mismatch|installed # # missing: run with pyarrow installed but not pyarrow-cloudfs. Each enabled # filesystem must fail with a message pointing at pyarrow-cloudfs, both # from pyarrow.fs and from FileSystem.from_uri. +# mismatch: run with pyarrow installed but not pyarrow-cloudfs. Installs an +# empty pyarrow-cloudfs of another version, as 'pip install -U pyarrow' +# leaves behind after upgrading from a placeholder release, checks that each +# enabled filesystem fails with the command to install the matching +# version, then uninstalls it again. # installed: run with both installed. FileSystem.from_uri must resolve each # enabled filesystem even if pyarrow.fs was never imported. # # A filesystem is checked when its PYARROW_TEST_ variable is ON. +import base64 +import hashlib import os +import subprocess import sys +import tempfile +import zipfile from pyarrow.lib import ArrowInvalid @@ -43,11 +53,83 @@ "?credential_kind=anonymous", "abfs"), ] +MISMATCHED_VERSION = "0.0.1" + + +def write_empty_cloudfs_wheel(directory, version): + """Write an empty pyarrow-cloudfs wheel, without needing a build backend.""" + dist_info = f"pyarrow_cloudfs-{version}.dist-info" + files = { + "pyarrow_cloudfs/__init__.py": b"", + f"{dist_info}/METADATA": ( + "Metadata-Version: 2.1\n" + "Name: pyarrow-cloudfs\n" + f"Version: {version}\n" + ).encode(), + f"{dist_info}/WHEEL": ( + "Wheel-Version: 1.0\n" + "Generator: arrow-ci\n" + "Root-Is-Purelib: true\n" + "Tag: py3-none-any\n" + ).encode(), + } + record = [] + for path, content in files.items(): + digest = base64.urlsafe_b64encode( + hashlib.sha256(content).digest()).rstrip(b"=").decode() + record.append(f"{path},sha256={digest},{len(content)}") + record.append(f"{dist_info}/RECORD,,") + files[f"{dist_info}/RECORD"] = ("\n".join(record) + "\n").encode() + + wheel_path = os.path.join( + directory, f"pyarrow_cloudfs-{version}-py3-none-any.whl") + with zipfile.ZipFile(wheel_path, "w") as wheel: + for path, content in files.items(): + wheel.writestr(path, content) + return wheel_path + + mode = sys.argv[1] enabled = [fs for fs in FILESYSTEMS if os.environ.get(fs[0]) == "ON"] +if mode == "mismatch": + pip = [sys.executable, "-m", "pip"] + with tempfile.TemporaryDirectory() as directory: + wheel_path = write_empty_cloudfs_wheel(directory, MISMATCHED_VERSION) + subprocess.run(pip + ["install", "--no-deps", wheel_path], check=True) + try: + # A fresh interpreter, so pyarrow.fs runs its check at import time. + subprocess.run([sys.executable, __file__, "mismatch-check"], check=True) + finally: + subprocess.run(pip + ["uninstall", "--yes", "pyarrow-cloudfs"], + check=True) + print(f"Checked mismatch: {[name for _, name, _, _ in enabled]}") + sys.exit(0) + +if mode == "mismatch-check": + import pyarrow + expected = f"pyarrow-cloudfs=={pyarrow.__version__}" + for _, name, uri, type_name in enabled: - if mode == "missing": + if mode == "mismatch-check": + from pyarrow._fs import FileSystem + try: + FileSystem.from_uri(uri) + except ArrowInvalid as e: + assert expected in str(e), e + else: + raise AssertionError(f"from_uri resolved {uri} with a mismatched " + "pyarrow-cloudfs") + + import pyarrow.fs as fs + try: + getattr(fs, name) + except ImportError as e: + assert expected in str(e), e + else: + raise AssertionError(f"{name} available with a mismatched " + "pyarrow-cloudfs") + elif mode == "missing": try: from pyarrow._fs import FileSystem FileSystem.from_uri(uri) @@ -70,4 +152,10 @@ else: raise ValueError(f"unknown mode: {mode}") +if mode == "mismatch-check": + # The extensions must not have been imported: their RPATH would let the + # loader pick up the mismatched libraries. + for module in ("pyarrow._s3fs", "pyarrow._gcsfs", "pyarrow._azurefs"): + assert module not in sys.modules, module + print(f"Checked {mode}: {[name for _, name, _, _ in enabled]}") diff --git a/ci/scripts/python_wheel_unix_test.sh b/ci/scripts/python_wheel_unix_test.sh index a2e0ef33c539..60abf41aed40 100755 --- a/ci/scripts/python_wheel_unix_test.sh +++ b/ci/scripts/python_wheel_unix_test.sh @@ -64,6 +64,7 @@ if [ "${INSTALL_PYARROW}" == "ON" ]; then python -m pip install "${source_dir}"/python/repaired_wheels/pyarrow-*.whl if [ "${PYARROW_TEST_S3}" == "ON" ] || [ "${PYARROW_TEST_GCS}" == "ON" ] || [ "${PYARROW_TEST_AZURE}" == "ON" ]; then python "${source_dir}/ci/scripts/python_wheel_check_cloudfs_split.py" missing + python "${source_dir}/ci/scripts/python_wheel_check_cloudfs_split.py" mismatch python -m pip install "${source_dir}"/python/repaired_wheels/pyarrow_cloudfs-*.whl python "${source_dir}/ci/scripts/python_wheel_check_cloudfs_split.py" installed fi diff --git a/ci/scripts/python_wheel_windows_test.bat b/ci/scripts/python_wheel_windows_test.bat index 8dd7243feeb1..d5f95ab1516b 100755 --- a/ci/scripts/python_wheel_windows_test.bat +++ b/ci/scripts/python_wheel_windows_test.bat @@ -46,6 +46,7 @@ py -0p @REM pyarrow installed, check that they fail with an actionable message, then @REM install it and check that they work. %PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_cloudfs_split.py missing || exit /B 1 +%PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_cloudfs_split.py mismatch || exit /B 1 %PYTHON_CMD% -m pip install --no-index --find-links=C:\arrow\python\repaired_wheels pyarrow-cloudfs || exit /B 1 %PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_cloudfs_split.py installed || exit /B 1 diff --git a/python/pyarrow-cloudfs/pyarrow_cloudfs/__init__.py b/python/pyarrow-cloudfs/pyarrow_cloudfs/__init__.py index 8d42bfbf1fc9..99c86a1d276c 100644 --- a/python/pyarrow-cloudfs/pyarrow_cloudfs/__init__.py +++ b/python/pyarrow-cloudfs/pyarrow_cloudfs/__init__.py @@ -26,11 +26,26 @@ import glob import os import sys +from importlib.metadata import version as _dist_version # These libraries depend on libarrow, which ships in the pyarrow wheel. # Importing pyarrow first loads libarrow into the process, so the dynamic # loader resolves that dependency by SONAME. -import pyarrow # noqa: F401 +import pyarrow + +__version__ = _dist_version("pyarrow-cloudfs") + +# The libraries are built from the same sources as pyarrow's extensions and +# only work with that exact build. A patch release keeps the library file +# names, so a mismatch would still load: compare the full version. +# pyarrow.fs checks this too before importing this package; this check covers +# importing it directly. +if __version__ != pyarrow.__version__: + raise ImportError( + f"pyarrow-cloudfs {__version__} requires pyarrow {__version__}, but " + f"pyarrow {pyarrow.__version__} is installed. Install matching " + f"versions with 'pip install \"pyarrow-cloudfs=={pyarrow.__version__}\"'" + ) def _pattern(name): diff --git a/python/pyarrow/fs.py b/python/pyarrow/fs.py index 3c6df8c1741c..6bf5358fdff1 100644 --- a/python/pyarrow/fs.py +++ b/python/pyarrow/fs.py @@ -41,28 +41,74 @@ _not_imported = [] _not_imported_reasons: dict[str, str] = {} -try: - # S3, GCS and Azure support can be installed separately, as the - # pyarrow-cloudfs package. Importing it loads libarrow_s3, libarrow_gcs - # and libarrow_azure so that pyarrow._s3fs, _gcsfs and _azurefs can be - # imported. - import pyarrow_cloudfs # noqa: F401 -except ModuleNotFoundError: + +class _CloudFsUnavailable(ImportError): pass +def _load_cloudfs(): + """ + Load pyarrow-cloudfs if it is installed and matches this pyarrow. + + PyPI wheels ship libarrow_s3, libarrow_gcs and libarrow_azure in the + separate pyarrow-cloudfs package. Other builds (conda, distros, source) + have no such package and link the libraries directly. + + Return a message saying why the installed pyarrow-cloudfs cannot be + used, or None. + """ + from importlib.metadata import PackageNotFoundError, version + from pyarrow import __version__ + try: + installed = version("pyarrow-cloudfs") + except PackageNotFoundError: + return None + if installed != __version__: + # The normal state after 'pip install -U pyarrow': pip upgrades + # pyarrow but not the pyarrow-cloudfs installed next to it. Compare + # the full version: a patch release keeps the library file names, so + # the loader would otherwise load the mismatched libraries. + return ( + f"pyarrow-cloudfs {installed} is installed, but pyarrow is " + f"{__version__}. Install the matching version with " + f"'pip install \"pyarrow-cloudfs=={__version__}\"'" + ) + try: + # Preloads the libraries so that pyarrow._s3fs, _gcsfs and _azurefs + # can resolve them. + import pyarrow_cloudfs # noqa: F401 + except (ImportError, OSError) as exc: + return f"pyarrow-cloudfs could not be loaded: {exc}" + return None + + +_cloudfs_error = _load_cloudfs() + + +def _require_cloudfs(): + # When pyarrow-cloudfs cannot be used, the extensions must not even be + # imported: their RPATH points into the sibling pyarrow_cloudfs + # directory, so the dynamic loader would find its libraries on its own. + if _cloudfs_error is not None: + raise _CloudFsUnavailable(_cloudfs_error) + + def _cloudfs_not_imported(name, extra, exc): _not_imported.append(name) - if not isinstance(exc, ModuleNotFoundError): + if isinstance(exc, _CloudFsUnavailable): + _not_imported_reasons[name] = str(exc) + elif not isinstance(exc, ModuleNotFoundError): # The extension exists, but its Arrow library could not be loaded. _not_imported_reasons[name] = ( f"{exc}. If pyarrow was installed from PyPI, this support is " "provided by the separate 'pyarrow-cloudfs' package: install it " f"with 'pip install \"pyarrow[{extra}]\"'" ) + # ModuleNotFoundError: pyarrow was built without it, nothing to add. try: + _require_cloudfs() from pyarrow._azurefs import AzureFileSystem # noqa except ImportError as exc: _cloudfs_not_imported("AzureFileSystem", "azure", exc) @@ -73,11 +119,13 @@ def _cloudfs_not_imported(name, extra, exc): _not_imported.append("HadoopFileSystem") try: + _require_cloudfs() from pyarrow._gcsfs import GcsFileSystem # noqa except ImportError as exc: _cloudfs_not_imported("GcsFileSystem", "gcs", exc) try: + _require_cloudfs() from pyarrow._s3fs import ( # noqa AwsDefaultS3RetryStrategy, AwsStandardS3RetryStrategy, S3FileSystem, S3LogLevel, S3RetryStrategy, ensure_s3_initialized, @@ -95,10 +143,11 @@ def _cloudfs_not_imported(name, extra, exc): def __getattr__(name): if name in _not_imported: - msg = f"The pyarrow installation is not built with support for '{name}'" if name in _not_imported_reasons: - msg += f" ({_not_imported_reasons[name]})" - raise ImportError(msg) + raise ImportError( + f"'{name}' is not available: {_not_imported_reasons[name]}") + raise ImportError( + f"The pyarrow installation is not built with support for '{name}'") raise AttributeError( f"module 'pyarrow.fs' has no attribute '{name}'" From 0e4393531de827fcc6a5e0c56fca0dc2bf6c834b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Fri, 4 Dec 2026 12:36:12 +0100 Subject: [PATCH 12/13] GH-38536: [Python][Packaging] PoC to split Cloud FS and Flight to its own wheels --- ci/scripts/python_wheel_check_flight_split.py | 46 ++++++++++++++++++ ci/scripts/python_wheel_macos_build.sh | 8 ++++ ci/scripts/python_wheel_unix_test.sh | 6 +++ ci/scripts/python_wheel_validate_contents.py | 3 +- ci/scripts/python_wheel_windows_build.bat | 20 +++++++- ci/scripts/python_wheel_windows_test.bat | 5 ++ ci/scripts/python_wheel_xlinux_build.sh | 15 +++++- python/CMakeLists.txt | 24 +++++----- python/pyarrow-flight/CMakeLists.txt | 46 ++++++++++++++++++ .../pyarrow-flight/pyarrow_flight/__init__.py | 46 ++++++++++++++++++ python/pyarrow-flight/pyproject.toml | 47 +++++++++++++++++++ python/pyarrow/flight.py | 20 ++++++-- python/pyproject.toml | 3 +- 13 files changed, 268 insertions(+), 21 deletions(-) create mode 100644 ci/scripts/python_wheel_check_flight_split.py create mode 100644 python/pyarrow-flight/CMakeLists.txt create mode 100644 python/pyarrow-flight/pyarrow_flight/__init__.py create mode 100644 python/pyarrow-flight/pyproject.toml diff --git a/ci/scripts/python_wheel_check_flight_split.py b/ci/scripts/python_wheel_check_flight_split.py new file mode 100644 index 000000000000..162a52b12a86 --- /dev/null +++ b/ci/scripts/python_wheel_check_flight_split.py @@ -0,0 +1,46 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Check Flight against the pyarrow-flight split. +# +# Usage: python_wheel_check_flight_split.py missing|installed +# +# missing: run with pyarrow installed but not pyarrow-flight. Importing +# pyarrow.flight must fail with a message pointing at pyarrow-flight. +# installed: run with both installed. pyarrow.flight must import and a server +# must start, which needs libarrow_flight and the gRPC it bundles. + +import sys + +mode = sys.argv[1] + +if mode == "missing": + try: + import pyarrow.flight # noqa: F401 + except ImportError as e: + assert "pyarrow-flight" in str(e), e + else: + raise AssertionError("pyarrow.flight available without pyarrow-flight") +elif mode == "installed": + import pyarrow.flight as flight + server = flight.FlightServerBase("grpc://localhost:0") + assert server.port > 0, server.port + server.shutdown() +else: + raise ValueError(f"unknown mode: {mode}") + +print(f"Checked {mode}: Flight") diff --git a/ci/scripts/python_wheel_macos_build.sh b/ci/scripts/python_wheel_macos_build.sh index 24e79f254414..e0b5cc5099d3 100755 --- a/ci/scripts/python_wheel_macos_build.sh +++ b/ci/scripts/python_wheel_macos_build.sh @@ -205,3 +205,11 @@ if [ "${ARROW_S3}" == "ON" ] || [ "${ARROW_GCS}" == "ON" ] || [ "${ARROW_AZURE}" -C cmake.define.PYARROW_CLOUDFS_AZURE="${ARROW_AZURE}" \ "${source_dir}/python/pyarrow-cloudfs" fi + +if [ "${ARROW_FLIGHT}" == "ON" ]; then + echo "=== (${PYTHON_VERSION}) Building pyarrow-flight wheel ===" + # CMake pulls libarrow_flight from the Arrow install via CMAKE_PREFIX_PATH. + python -m build --wheel --no-isolation \ + --outdir "${source_dir}/python/repaired_wheels" \ + "${source_dir}/python/pyarrow-flight" +fi diff --git a/ci/scripts/python_wheel_unix_test.sh b/ci/scripts/python_wheel_unix_test.sh index 60abf41aed40..da5f7396b99a 100755 --- a/ci/scripts/python_wheel_unix_test.sh +++ b/ci/scripts/python_wheel_unix_test.sh @@ -68,6 +68,12 @@ if [ "${INSTALL_PYARROW}" == "ON" ]; then python -m pip install "${source_dir}"/python/repaired_wheels/pyarrow_cloudfs-*.whl python "${source_dir}/ci/scripts/python_wheel_check_cloudfs_split.py" installed fi + # Flight ships in the separate pyarrow-flight wheel. + if [ "${PYARROW_TEST_FLIGHT}" == "ON" ]; then + python "${source_dir}/ci/scripts/python_wheel_check_flight_split.py" missing + python -m pip install "${source_dir}"/python/repaired_wheels/pyarrow_flight-*.whl + python "${source_dir}/ci/scripts/python_wheel_check_flight_split.py" installed + fi fi if [ "${CHECK_IMPORTS}" == "ON" ]; then diff --git a/ci/scripts/python_wheel_validate_contents.py b/ci/scripts/python_wheel_validate_contents.py index aa21137b96ce..95696dc2344d 100644 --- a/ci/scripts/python_wheel_validate_contents.py +++ b/ci/scripts/python_wheel_validate_contents.py @@ -40,7 +40,8 @@ def _count_docstrings(source): # TODO(GH-48970): Check stubs ARE present once annotations are complete def validate_wheel(path): p = Path(path) - # Only the core pyarrow wheel; pyarrow_cloudfs-*.whl sits alongside it. + # Only the core pyarrow wheel; pyarrow_cloudfs-*.whl and + # pyarrow_flight-*.whl sit alongside it. wheels = list(p.glob('pyarrow-*.whl')) error_msg = f"{len(wheels)} wheels found but only 1 expected ({wheels})" assert len(wheels) == 1, error_msg diff --git a/ci/scripts/python_wheel_windows_build.bat b/ci/scripts/python_wheel_windows_build.bat index 6a6761c56792..fa2e3b282707 100644 --- a/ci/scripts/python_wheel_windows_build.bat +++ b/ci/scripts/python_wheel_windows_build.bat @@ -152,10 +152,10 @@ for /f %%i in ('dir dist\pyarrow-*.whl /B') do (set WHEEL_NAME=%cd%\dist\%%i) || echo "Wheel name: %WHEEL_NAME%" @REM arrow_s3.dll, arrow_gcs.dll and arrow_azure.dll ship in the separate -@REM pyarrow-cloudfs wheel. +@REM pyarrow-cloudfs wheel, arrow_flight.dll in the pyarrow-flight wheel. %PYTHON_CMD% -m delvewheel repair -vv ^ --ignore-existing --with-mangle ^ - --exclude "arrow_s3.dll;arrow_gcs.dll;arrow_azure.dll" ^ + --exclude "arrow_s3.dll;arrow_gcs.dll;arrow_azure.dll;arrow_flight.dll" ^ -w repaired_wheels %WHEEL_NAME% || exit /B 1 set BUILD_CLOUDFS=OFF @@ -182,4 +182,20 @@ echo "pyarrow-cloudfs wheel name: %CLOUDFS_WHEEL_NAME%" :skip_pyarrow_cloudfs +if not "%ARROW_FLIGHT%"=="ON" goto :skip_pyarrow_flight + +@REM Build pyarrow-flight: CMake pulls arrow_flight.dll from C:\arrow-dist via +@REM CMAKE_PREFIX_PATH. +%PYTHON_CMD% -m build --wheel --no-isolation --outdir dist-flight pyarrow-flight || exit /B 1 + +for /f %%i in ('dir dist-flight\pyarrow_flight-*.whl /B') do (set FLIGHT_WHEEL_NAME=%cd%\dist-flight\%%i) || exit /B 1 +echo "pyarrow-flight wheel name: %FLIGHT_WHEEL_NAME%" + +@REM arrow.dll ships in the pyarrow wheel. +%PYTHON_CMD% -m delvewheel repair -vv ^ + --ignore-existing --analyze-existing --with-mangle --exclude arrow.dll ^ + -w repaired_wheels %FLIGHT_WHEEL_NAME% || exit /B 1 + +:skip_pyarrow_flight + popd diff --git a/ci/scripts/python_wheel_windows_test.bat b/ci/scripts/python_wheel_windows_test.bat index d5f95ab1516b..6ed869765f01 100755 --- a/ci/scripts/python_wheel_windows_test.bat +++ b/ci/scripts/python_wheel_windows_test.bat @@ -50,6 +50,11 @@ py -0p %PYTHON_CMD% -m pip install --no-index --find-links=C:\arrow\python\repaired_wheels pyarrow-cloudfs || exit /B 1 %PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_cloudfs_split.py installed || exit /B 1 +@REM Flight ships in the separate pyarrow-flight wheel. +%PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_flight_split.py missing || exit /B 1 +%PYTHON_CMD% -m pip install --no-index --find-links=C:\arrow\python\repaired_wheels pyarrow-flight || exit /B 1 +%PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_flight_split.py installed || exit /B 1 + @REM Test that the modules are importable %PYTHON_CMD% -c "import pyarrow" || exit /B 1 %PYTHON_CMD% -c "import pyarrow.fs; pyarrow.fs.AzureFileSystem" || exit /B 1 diff --git a/ci/scripts/python_wheel_xlinux_build.sh b/ci/scripts/python_wheel_xlinux_build.sh index 75943778bee8..96b7c3b659df 100755 --- a/ci/scripts/python_wheel_xlinux_build.sh +++ b/ci/scripts/python_wheel_xlinux_build.sh @@ -241,15 +241,26 @@ if [ "${build_cloudfs}" == "ON" ]; then pyarrow-cloudfs fi +if [ "${ARROW_FLIGHT}" == "ON" ]; then + echo "=== (${PYTHON_VERSION}) Building pyarrow-flight wheel ===" + # CMake pulls libarrow_flight from /tmp/arrow-dist via CMAKE_PREFIX_PATH. + python -m build --wheel --no-isolation --outdir dist pyarrow-flight +fi + echo "=== (${PYTHON_VERSION}) Tag the wheel with ${LINUX_WHEEL_KIND}${LINUX_WHEEL_VERSION} ===" -# libarrow ships in pyarrow, and libarrow_s3, libarrow_gcs and libarrow_azure -# in pyarrow-cloudfs: neither wheel may graft the other's libraries. +# libarrow ships in pyarrow, libarrow_s3, libarrow_gcs and libarrow_azure in +# pyarrow-cloudfs, and libarrow_flight in pyarrow-flight: no wheel may graft +# another's libraries. auditwheel repair \ --exclude 'libarrow_s3.so.*' \ --exclude 'libarrow_gcs.so.*' \ --exclude 'libarrow_azure.so.*' \ + --exclude 'libarrow_flight.so.*' \ dist/pyarrow-*.whl -w repaired_wheels if [ "${build_cloudfs}" == "ON" ]; then auditwheel repair --exclude 'libarrow.so.*' dist/pyarrow_cloudfs-*.whl -w repaired_wheels fi +if [ "${ARROW_FLIGHT}" == "ON" ]; then + auditwheel repair --exclude 'libarrow.so.*' dist/pyarrow_flight-*.whl -w repaired_wheels +fi popd diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 4d1211a7aa53..1cc561065c97 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -883,18 +883,8 @@ endif() # Flight if(PYARROW_BUILD_FLIGHT) - if(PYARROW_BUNDLE_ARROW_CPP) - bundle_arrow_lib(${ARROW_FLIGHT_SHARED_LIB} SO_VERSION ${ARROW_SO_VERSION}) - if(MSVC) - bundle_arrow_import_lib(${ARROW_FLIGHT_IMPORT_LIB}) - # XXX Hardcoded library names because CMake is too stupid to give us - # the shared library paths. - # https://gitlab.kitware.com/cmake/cmake/issues/16210 - # bundle_arrow_dependency(libcrypto-1_1-x64) - # bundle_arrow_dependency(libssl-1_1-x64) - endif() - endif() - + # libarrow_flight is not bundled in the PyArrow wheel: it ships in the + # separate pyarrow-flight wheel (python/pyarrow-flight). set(FLIGHT_LINK_LIBS arrow_python_flight) list(APPEND CYTHON_EXTENSIONS _flight) else() @@ -1091,6 +1081,16 @@ if(PYARROW_BUNDLE_ARROW_CPP AND UNIX) PROPERTY INSTALL_RPATH "${PYARROW_RPATH_ORIGIN}/../pyarrow_cloudfs") endif() endforeach() + # Likewise for libarrow_flight, which ships in the sibling pyarrow-flight + # wheel. arrow_python_flight carries the DT_NEEDED entry on it, and _flight + # links it through arrow_python_flight. + foreach(flight_target arrow_python_flight _flight) + if(TARGET ${flight_target}) + set_property(TARGET ${flight_target} + APPEND + PROPERTY INSTALL_RPATH "${PYARROW_RPATH_ORIGIN}/../pyarrow_flight") + endif() + endforeach() endif() # diff --git a/python/pyarrow-flight/CMakeLists.txt b/python/pyarrow-flight/CMakeLists.txt new file mode 100644 index 000000000000..36e69d18825b --- /dev/null +++ b/python/pyarrow-flight/CMakeLists.txt @@ -0,0 +1,46 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +cmake_minimum_required(VERSION 3.25) + +# Nothing is compiled, but Arrow's CMake packages expect a language enabled. +project(pyarrow_flight LANGUAGES CXX) + +find_package(ArrowFlight REQUIRED) + +# Install libarrow_flight under the name pyarrow's arrow_python_flight looks +# it up by: its SONAME on Linux, its install name on macOS, the DLL name on +# Windows. The naming matches bundle_arrow_lib in python/CMakeLists.txt. +get_filename_component(lib_real "${ARROW_FLIGHT_SHARED_LIB}" REALPATH) +get_filename_component(lib_name "${ARROW_FLIGHT_SHARED_LIB}" NAME_WE) +if(MSVC) + set(installed_name "${lib_name}${CMAKE_SHARED_LIBRARY_SUFFIX}") +elseif(APPLE) + set(installed_name "${lib_name}.${ARROW_SO_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}") +else() + set(installed_name "${lib_name}${CMAKE_SHARED_LIBRARY_SUFFIX}.${ARROW_SO_VERSION}") +endif() +install(FILES "${lib_real}" + DESTINATION pyarrow_flight + RENAME "${installed_name}") +if(MSVC) + # The import library, previously bundled in the pyarrow wheel, for C++ + # extensions that link against Arrow Flight. + install(FILES "${ARROW_FLIGHT_IMPORT_LIB}" + DESTINATION pyarrow_flight + RENAME "${lib_name}.lib") +endif() diff --git a/python/pyarrow-flight/pyarrow_flight/__init__.py b/python/pyarrow-flight/pyarrow_flight/__init__.py new file mode 100644 index 000000000000..18193524288b --- /dev/null +++ b/python/pyarrow-flight/pyarrow_flight/__init__.py @@ -0,0 +1,46 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +Arrow Flight RPC support for PyArrow wheels. + +pyarrow imports this package itself, from pyarrow.flight. Importing it +directly is not needed. +""" + +import ctypes +import glob +import os +import sys + +# libarrow_flight depends on libarrow, which ships in the pyarrow wheel. +# Importing pyarrow first loads libarrow into the process, so the dynamic +# loader resolves that dependency by SONAME. +import pyarrow # noqa: F401 + +if sys.platform == "win32": + _pattern = "arrow_flight.dll" +elif sys.platform == "darwin": + _pattern = "libarrow_flight.*.dylib" +else: + _pattern = "libarrow_flight.so.*" + +# Keep a reference so the library stays loaded for the process lifetime. +_libarrow_flight = ctypes.CDLL( + glob.glob(os.path.join(os.path.dirname(__file__), _pattern))[0], + mode=ctypes.RTLD_GLOBAL, +) diff --git a/python/pyarrow-flight/pyproject.toml b/python/pyarrow-flight/pyproject.toml new file mode 100644 index 000000000000..7b4569d25e42 --- /dev/null +++ b/python/pyarrow-flight/pyproject.toml @@ -0,0 +1,47 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +[build-system] +requires = ["scikit-build-core>=1.0", "setuptools_scm[toml]>=8"] +build-backend = "scikit_build_core.build" + +[project] +name = "pyarrow-flight" +dynamic = ["version", "dependencies"] +requires-python = ">=3.11" +description = "Arrow Flight RPC support for PyArrow" +license = "Apache-2.0" +# TODO: Add the license files + +[tool.scikit-build] +metadata.version.provider = "scikit_build_core.metadata.setuptools_scm" +wheel.packages = ["pyarrow_flight"] +# The wheel ships a shared library but no CPython extension: one wheel per +# platform (py3-none-) installed to platlib, next to pyarrow. +wheel.py-api = "py3" +wheel.platlib = true + +# Pin pyarrow to exactly this package's version. +[tool.scikit-build.metadata.dependencies] +provider = "scikit_build_core.metadata.template" +result = ["pyarrow=={project[version]}"] + +[tool.setuptools_scm] +root = '../..' +version_scheme = 'guess-next-dev' +git_describe_command = 'git describe --dirty --tags --long --match "apache-arrow-[0-9]*.*"' +fallback_version = '26.0.0a0' diff --git a/python/pyarrow/flight.py b/python/pyarrow/flight.py index b1836907c674..99b916335f77 100644 --- a/python/pyarrow/flight.py +++ b/python/pyarrow/flight.py @@ -15,6 +15,14 @@ # specific language governing permissions and limitations # under the License. +try: + # Flight support can be installed separately, as the pyarrow-flight + # package. Importing it loads libarrow_flight so that pyarrow._flight can + # be imported. + import pyarrow_flight # noqa: F401 +except ModuleNotFoundError: + pass + try: from pyarrow._flight import ( # noqa:F401 connect, @@ -64,6 +72,12 @@ TracingServerMiddlewareFactory, ) except ImportError as exc: - raise ImportError( - f"The pyarrow installation is not built with support for 'flight' ({str(exc)})" - ) from None + msg = f"The pyarrow installation is not built with support for 'flight' ({exc})" + if not isinstance(exc, ModuleNotFoundError): + # pyarrow._flight exists, but libarrow_flight could not be loaded. + msg += ( + ". If pyarrow was installed from PyPI, Flight support is provided " + "by the separate 'pyarrow-flight' package: install it with " + "'pip install \"pyarrow[flight]\"'" + ) + raise ImportError(msg) from None diff --git a/python/pyproject.toml b/python/pyproject.toml index 3d36e569f531..89592ff97aec 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -112,6 +112,7 @@ provider = "scikit_build_core.metadata.template" s3 = ["pyarrow-cloudfs=={project[version]}"] gcs = ["pyarrow-cloudfs=={project[version]}"] azure = ["pyarrow-cloudfs=={project[version]}"] +flight = ["pyarrow-flight=={project[version]}"] [tool.setuptools_scm] root = '..' @@ -137,7 +138,7 @@ follow_imports = "skip" # Optional dependencies without type hints. [[tool.mypy.overrides]] -module = ["fsspec", "pyarrow_cloudfs", "sparse"] +module = ["fsspec", "pyarrow_cloudfs", "pyarrow_flight", "sparse"] ignore_missing_imports = true [tool.pyright] From 07286f1f42f6293c516c441c6bfaed876bd8a742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Cumplido?= Date: Fri, 4 Dec 2026 16:40:07 +0100 Subject: [PATCH 13/13] Test mistmatch wheel --- .../python_wheel_check_cloudfs_split.py | 44 ++----------------- ci/scripts/python_wheel_check_flight_split.py | 37 +++++++++++++++- ci/scripts/python_wheel_unix_test.sh | 1 + ci/scripts/python_wheel_windows_test.bat | 1 + .../pyarrow-flight/pyarrow_flight/__init__.py | 17 ++++++- python/pyarrow/flight.py | 28 ++++++++++-- 6 files changed, 81 insertions(+), 47 deletions(-) diff --git a/ci/scripts/python_wheel_check_cloudfs_split.py b/ci/scripts/python_wheel_check_cloudfs_split.py index 09d172a317c7..4647826f7c48 100644 --- a/ci/scripts/python_wheel_check_cloudfs_split.py +++ b/ci/scripts/python_wheel_check_cloudfs_split.py @@ -32,16 +32,15 @@ # # A filesystem is checked when its PYARROW_TEST_ variable is ON. -import base64 -import hashlib import os import subprocess import sys import tempfile -import zipfile from pyarrow.lib import ArrowInvalid +from python_wheel_empty_wheel import MISMATCHED_VERSION, write_empty_wheel + # (PYARROW_TEST_ variable, pyarrow.fs name, URI, FileSystem.type_name) FILESYSTEMS = [ ("PYARROW_TEST_S3", "S3FileSystem", @@ -53,49 +52,14 @@ "?credential_kind=anonymous", "abfs"), ] -MISMATCHED_VERSION = "0.0.1" - - -def write_empty_cloudfs_wheel(directory, version): - """Write an empty pyarrow-cloudfs wheel, without needing a build backend.""" - dist_info = f"pyarrow_cloudfs-{version}.dist-info" - files = { - "pyarrow_cloudfs/__init__.py": b"", - f"{dist_info}/METADATA": ( - "Metadata-Version: 2.1\n" - "Name: pyarrow-cloudfs\n" - f"Version: {version}\n" - ).encode(), - f"{dist_info}/WHEEL": ( - "Wheel-Version: 1.0\n" - "Generator: arrow-ci\n" - "Root-Is-Purelib: true\n" - "Tag: py3-none-any\n" - ).encode(), - } - record = [] - for path, content in files.items(): - digest = base64.urlsafe_b64encode( - hashlib.sha256(content).digest()).rstrip(b"=").decode() - record.append(f"{path},sha256={digest},{len(content)}") - record.append(f"{dist_info}/RECORD,,") - files[f"{dist_info}/RECORD"] = ("\n".join(record) + "\n").encode() - - wheel_path = os.path.join( - directory, f"pyarrow_cloudfs-{version}-py3-none-any.whl") - with zipfile.ZipFile(wheel_path, "w") as wheel: - for path, content in files.items(): - wheel.writestr(path, content) - return wheel_path - - mode = sys.argv[1] enabled = [fs for fs in FILESYSTEMS if os.environ.get(fs[0]) == "ON"] if mode == "mismatch": pip = [sys.executable, "-m", "pip"] with tempfile.TemporaryDirectory() as directory: - wheel_path = write_empty_cloudfs_wheel(directory, MISMATCHED_VERSION) + wheel_path = write_empty_wheel(directory, "pyarrow-cloudfs", + MISMATCHED_VERSION) subprocess.run(pip + ["install", "--no-deps", wheel_path], check=True) try: # A fresh interpreter, so pyarrow.fs runs its check at import time. diff --git a/ci/scripts/python_wheel_check_flight_split.py b/ci/scripts/python_wheel_check_flight_split.py index 162a52b12a86..041ee2287883 100644 --- a/ci/scripts/python_wheel_check_flight_split.py +++ b/ci/scripts/python_wheel_check_flight_split.py @@ -17,18 +17,51 @@ # Check Flight against the pyarrow-flight split. # -# Usage: python_wheel_check_flight_split.py missing|installed +# Usage: python_wheel_check_flight_split.py missing|mismatch|installed # # missing: run with pyarrow installed but not pyarrow-flight. Importing # pyarrow.flight must fail with a message pointing at pyarrow-flight. +# mismatch: run with pyarrow installed but not pyarrow-flight. Installs an +# empty pyarrow-flight of another version, checks that importing +# pyarrow.flight fails with the command to install the matching version +# without importing pyarrow._flight, then uninstalls it again. # installed: run with both installed. pyarrow.flight must import and a server # must start, which needs libarrow_flight and the gRPC it bundles. +import subprocess import sys +import tempfile + +from python_wheel_empty_wheel import MISMATCHED_VERSION, write_empty_wheel mode = sys.argv[1] -if mode == "missing": +if mode == "mismatch": + pip = [sys.executable, "-m", "pip"] + with tempfile.TemporaryDirectory() as directory: + wheel_path = write_empty_wheel(directory, "pyarrow-flight", + MISMATCHED_VERSION) + subprocess.run(pip + ["install", "--no-deps", wheel_path], check=True) + try: + # A fresh interpreter, so pyarrow.flight runs its check at import. + subprocess.run([sys.executable, __file__, "mismatch-check"], check=True) + finally: + subprocess.run(pip + ["uninstall", "--yes", "pyarrow-flight"], + check=True) +elif mode == "mismatch-check": + import pyarrow + expected = f"pyarrow-flight=={pyarrow.__version__}" + try: + import pyarrow.flight # noqa: F401 + except ImportError as e: + assert expected in str(e), e + else: + raise AssertionError("pyarrow.flight available with a mismatched " + "pyarrow-flight") + # The extension must not have been imported: its RPATH would let the + # loader pick up the mismatched library. + assert "pyarrow._flight" not in sys.modules +elif mode == "missing": try: import pyarrow.flight # noqa: F401 except ImportError as e: diff --git a/ci/scripts/python_wheel_unix_test.sh b/ci/scripts/python_wheel_unix_test.sh index da5f7396b99a..a60b30cdb9e8 100755 --- a/ci/scripts/python_wheel_unix_test.sh +++ b/ci/scripts/python_wheel_unix_test.sh @@ -71,6 +71,7 @@ if [ "${INSTALL_PYARROW}" == "ON" ]; then # Flight ships in the separate pyarrow-flight wheel. if [ "${PYARROW_TEST_FLIGHT}" == "ON" ]; then python "${source_dir}/ci/scripts/python_wheel_check_flight_split.py" missing + python "${source_dir}/ci/scripts/python_wheel_check_flight_split.py" mismatch python -m pip install "${source_dir}"/python/repaired_wheels/pyarrow_flight-*.whl python "${source_dir}/ci/scripts/python_wheel_check_flight_split.py" installed fi diff --git a/ci/scripts/python_wheel_windows_test.bat b/ci/scripts/python_wheel_windows_test.bat index 6ed869765f01..d305895c4944 100755 --- a/ci/scripts/python_wheel_windows_test.bat +++ b/ci/scripts/python_wheel_windows_test.bat @@ -52,6 +52,7 @@ py -0p @REM Flight ships in the separate pyarrow-flight wheel. %PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_flight_split.py missing || exit /B 1 +%PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_flight_split.py mismatch || exit /B 1 %PYTHON_CMD% -m pip install --no-index --find-links=C:\arrow\python\repaired_wheels pyarrow-flight || exit /B 1 %PYTHON_CMD% C:\arrow\ci\scripts\python_wheel_check_flight_split.py installed || exit /B 1 diff --git a/python/pyarrow-flight/pyarrow_flight/__init__.py b/python/pyarrow-flight/pyarrow_flight/__init__.py index 18193524288b..419c414429f0 100644 --- a/python/pyarrow-flight/pyarrow_flight/__init__.py +++ b/python/pyarrow-flight/pyarrow_flight/__init__.py @@ -26,11 +26,26 @@ import glob import os import sys +from importlib.metadata import version as _dist_version # libarrow_flight depends on libarrow, which ships in the pyarrow wheel. # Importing pyarrow first loads libarrow into the process, so the dynamic # loader resolves that dependency by SONAME. -import pyarrow # noqa: F401 +import pyarrow + +__version__ = _dist_version("pyarrow-flight") + +# libarrow_flight is built from the same sources as pyarrow's extensions and +# only works with that exact build. A patch release keeps the library file +# names, so a mismatch would still load: compare the full version. +# pyarrow.flight checks this too before importing this package; this check +# covers importing it directly. +if __version__ != pyarrow.__version__: + raise ImportError( + f"pyarrow-flight {__version__} requires pyarrow {__version__}, but " + f"pyarrow {pyarrow.__version__} is installed. Install matching " + f"versions with 'pip install \"pyarrow-flight=={pyarrow.__version__}\"'" + ) if sys.platform == "win32": _pattern = "arrow_flight.dll" diff --git a/python/pyarrow/flight.py b/python/pyarrow/flight.py index 99b916335f77..36db721f80e5 100644 --- a/python/pyarrow/flight.py +++ b/python/pyarrow/flight.py @@ -15,13 +15,33 @@ # specific language governing permissions and limitations # under the License. -try: +def _load_pyarrow_flight(): # Flight support can be installed separately, as the pyarrow-flight # package. Importing it loads libarrow_flight so that pyarrow._flight can # be imported. - import pyarrow_flight # noqa: F401 -except ModuleNotFoundError: - pass + from importlib.metadata import PackageNotFoundError, version + from pyarrow import __version__ + try: + installed = version("pyarrow-flight") + except PackageNotFoundError: + return + # libarrow_flight only works with the libarrow of the exact same build. + # A patch release keeps the library file names, so check the version + # before importing: pyarrow._flight would load a mismatched library. + if installed != __version__: + raise ImportError( + f"pyarrow-flight {installed} is installed, but pyarrow is " + f"{__version__}. Install the matching version with " + f"'pip install \"pyarrow-flight=={__version__}\"'" + ) + try: + import pyarrow_flight # noqa: F401 + except (ImportError, OSError) as exc: + raise ImportError( + f"pyarrow-flight could not be loaded: {exc}") from None + + +_load_pyarrow_flight() try: from pyarrow._flight import ( # noqa:F401