docker-library / python

Docker Official Image packaging for Python
https://www.python.org/
MIT License
2.54k stars 1.06k forks source link

is python:latest broken (mix 3.11 / 3.12) ? #971

Open plog opened 3 days ago

plog commented 3 days ago

I'm confused....

root@960f60076a7f:/app# ls /usr/bin/py*
/usr/bin/py3clean    /usr/bin/py3versions  /usr/bin/pydoc3.11   /usr/bin/pygettext3.11  /usr/bin/python3.11
/usr/bin/py3compile  /usr/bin/pydoc3       /usr/bin/pygettext3  /usr/bin/python3

root@960f60076a7f:/app# python
Python 3.12.7 (main, Oct  1 2024, 22:28:30) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

In some .py script

#!/usr/bin/env python3
...
logger.info(f"Python path: {sys.path}")
...

returns

[
'/app', 
'/usr/lib/python311.zip', 
'/usr/lib/python3.11', 
'/usr/lib/python3.11/lib-dynload', 
'/usr/local/lib/python3.11/dist-packages', 
'/usr/lib/python3/dist-packages'
]

Checking a random "pip (or pip3) install ...."

root@960f60076a7f:/app# pip show bleach
Name: bleach
Version: 6.1.0
Summary: An easy safelist-based HTML-sanitizing tool.
Home-page: https://github.com/mozilla/bleach
Author: 
Author-email: 
License: Apache Software License
Location: /usr/local/lib/python3.12/site-packages
Requires: six, webencodings
Required-by: 
edmorley commented 3 days ago

Hi!

The python:latest tag is currently an alias for 3.12.7-bookworm. The 3.12.7-bookworm image uses buildpack-deps:bookworm as it's base: https://github.com/docker-library/python/blob/4babb0e3da12a080e249f0d15c61404ac2e5d3b0/3.12/bookworm/Dockerfile#L7C6-L7C29

...and buildpack-deps:bookworm like many other of the larger images includes a system Python installation:

$ docker run --rm -it buildpack-deps:bookworm bash -c 'which python3 && python3 --version'
/usr/bin/python3
Python 3.11.2

However, that system install won't be used by default, since the Python images put their own Python install ahead of the system install on PATH: https://github.com/docker-library/python/blob/24f7d17744c3bceceb3d60e86c81b2e15a8c3104/3.12/bookworm/Dockerfile#L9-L10

Which can be confirmed by running:

$ docker pull -q python:latest && docker run --rm -it python:latest bash
docker.io/library/python:latest
root@6b11c0285af3:/# which python3
/usr/local/bin/python3
root@6b11c0285af3:/# python3 -m site
sys.path = [
    '/',
    '/usr/local/lib/python312.zip',
    '/usr/local/lib/python3.12',
    '/usr/local/lib/python3.12/lib-dynload',
    '/usr/local/lib/python3.12/site-packages',
]
USER_BASE: '/root/.local' (doesn't exist)
USER_SITE: '/root/.local/lib/python3.12/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
root@6b11c0285af3:/# pip install -q bleach
...
root@6b11c0285af3:/# pip show bleach
Name: bleach
Version: 6.1.0
...
Location: /usr/local/lib/python3.12/site-packages
...

If you are seeing something different, it means you have likely overridden PATH and put the /usr/bin/ location ahead of /usr/local/bin.

Fix the misconfigured PATH and the issue will go away.

Alternatively, switch to one of the slim Python images since they don't contain a system Python install:

$ docker pull -q python:3.12-slim && docker run --rm -it python:3.12-slim which -a python3
docker.io/library/python:3.12-slim
/usr/local/bin/python3
/usr/local/bin/python3
edmorley commented 3 days ago

This is also documented here: https://hub.docker.com/_/python/#:~:text=Multiple%20Python%20versions%20in%20the%20image