Skip to content

Commit 2ed523b

Browse files
authored
Merge pull request #39699 from apache/cp-39683
[Cherrypick] fix ensurepip bundled pip cleanup for Python 3.12+ containers
2 parents 8e16001 + 857caa6 commit 2ed523b

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

sdks/python/container/Dockerfile

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ RUN \
4949

5050
rm -rf /var/lib/apt/lists/* && \
5151

52-
pip install --upgrade pip setuptools wheel && \
53-
5452
# Install required packages for Beam Python SDK and common dependencies used by users.
5553
# use --no-deps to ensure the list includes all transitive dependencies.
5654
# use --prefer-binary to avoid compiling wheels from source when prebuilt wheels exist.
@@ -92,17 +90,13 @@ RUN \
9290
if [ "${py_version}" = "3.10" ] || [ "${py_version}" = "3.11" ]; then \
9391
pip install upgrade_ensurepip; \
9492
python3 -m upgrade_ensurepip; \
95-
else \
96-
python3 /tmp/upgrade_bundled_pip.py; \
97-
fi; \
98-
# setuptools is not bundled with ensurepip in Python 3.12+
99-
if [ "${py_version}" = "3.10" ] || [ "${py_version}" = "3.11" ]; then \
10093
find /usr/local/lib/python${py_version}/ensurepip/_bundled/setuptools-* -type f ! -name $(basename $(ls -v /usr/local/lib/python${py_version}/ensurepip/_bundled/setuptools-*-py3-none-any.whl | tail -n 1)) -delete; \
101-
fi; \
102-
find /usr/local/lib/python${py_version}/ensurepip/_bundled/pip-* -type f ! -name $(basename $(ls -v /usr/local/lib/python${py_version}/ensurepip/_bundled/pip-*-py3-none-any.whl | tail -n 1)) -delete; \
103-
if [ "${py_version}" = "3.10" ] || [ "${py_version}" = "3.11" ]; then \
94+
find /usr/local/lib/python${py_version}/ensurepip/_bundled/pip-* -type f ! -name $(basename $(ls -v /usr/local/lib/python${py_version}/ensurepip/_bundled/pip-*-py3-none-any.whl | tail -n 1)) -delete; \
10495
pip uninstall upgrade_ensurepip -y; \
96+
else \
97+
python3 /tmp/upgrade_bundled_pip.py; \
10598
fi; \
99+
# Verify ensurepip can bootstrap pip. Required by boot.go worker venv creation.
106100
python3 -m ensurepip;
107101

108102
ENTRYPOINT ["/opt/apache/beam/boot"]

sdks/python/container/license_scripts/upgrade_bundled_pip.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
The script is executed within Docker after the image pip has been upgraded.
2222
upgrade_ensurepip expects setuptools to be bundled as well, but Python 3.12+
2323
only ships pip in ensurepip/_bundled.
24+
25+
After downloading, removes other pip-* files from _bundled so only the wheel
26+
matching the installed pip version remains. Worker harness startup (boot.go)
27+
creates a venv via ensurepip, so that wheel must be present.
2428
"""
2529

2630
import subprocess
@@ -34,8 +38,8 @@ def main():
3438
ep_path = Path(ensurepip.__file__)
3539
wheel_dir = ep_path.parent / '_bundled'
3640
pip_version = subprocess.check_output(
37-
[sys.executable, '-m', 'pip', '--version'],
38-
text=True).split()[1]
41+
[sys.executable, '-m', 'pip', '--version'], text=True).split()[1]
42+
expected_wheel_name = 'pip-{}-py3-none-any.whl'.format(pip_version)
3943
subprocess.check_call([
4044
sys.executable,
4145
'-m',
@@ -46,6 +50,13 @@ def main():
4650
str(wheel_dir),
4751
'--no-deps',
4852
])
53+
for path in wheel_dir.glob('pip-*'):
54+
if path.name != expected_wheel_name:
55+
path.unlink()
56+
if not (wheel_dir / expected_wheel_name).is_file():
57+
sys.exit(
58+
'ensurepip bundled pip wheel missing after install: {}'.format(
59+
wheel_dir / expected_wheel_name))
4960
lines = ep_path.read_text().splitlines()
5061
pip_line = None
5162
for idx, line in enumerate(lines):

0 commit comments

Comments
 (0)