python / cpython

The Python programming language
https://www.python.org
Other
62.95k stars 30.15k forks source link

Deprecation of `Py_GetPrefix`-family APIs should document the `sys.base_*` counterparts #125313

Open y5c4l3 opened 3 days ago

y5c4l3 commented 3 days ago

Documentation

Documentation suggests sys.prefix as an alternative to Py_GetPrefix in the deprecation note here

Deprecated since version 3.13, will be removed in version 3.15: Get sys.prefix instead.

and it seems correct:

>>> import poc # CPython module that prints `Py_GetPrefix` on init
Prefix: /home/y5/micromamba/envs/py3.13
>>> import sys; print(sys.prefix); print(sys.base_prefix)
/home/y5/micromamba/envs/py3.13
/home/y5/micromamba/envs/py3.13

But strictly speaking, sys.base_prefix is the real counterpart rather than sys.prefix when it involves venv:

# inside venv
>>> import poc
Prefix: /home/y5/micromamba/envs/py3.13
>>> import sys; print(sys.prefix); print(sys.base_prefix)
/tmp/foo
/home/y5/micromamba/envs/py3.13

Source: #125235

Linked PRs

y5c4l3 commented 3 days ago

A fuller comparison for reference:

>>> import poc
... import sys
... for attr in ['executable', 'path', 'prefix', 'base_prefix', 'exec_prefix', 'base_exec_prefix']: print(f'sys.{attr}:', getattr(sys, attr))
...
Py_GetProgramName: python
Py_GetProgramFullPath: /home/y5/micromamba/envs/py3.13/bin/python
Py_GetPath: /home/y5/micromamba/envs/py3.13/lib/python313.zip:/home/y5/micromamba/envs/py3.13/lib/python3.13:/home/y5/micromamba/envs/py3.13/lib/python3.13/lib-dynload
Py_GetPrefix: /home/y5/micromamba/envs/py3.13
Py_GetExecPrefix: /home/y5/micromamba/envs/py3.13
sys.executable: /home/y5/micromamba/envs/py3.13/bin/python
sys.path: ['', '/home/y5/micromamba/envs/py3.13/lib/python313.zip', '/home/y5/micromamba/envs/py3.13/lib/python3.13', '/home/y5/micromamba/envs/py3.13/lib/python3.13/lib-dynload', '/home/y5/micromamba/envs/py3.13/lib/python3.13/site-packages']
sys.prefix: /home/y5/micromamba/envs/py3.13
sys.base_prefix: /home/y5/micromamba/envs/py3.13
sys.exec_prefix: /home/y5/micromamba/envs/py3.13
sys.base_exec_prefix: /home/y5/micromamba/envs/py3.13
# inside venv
>>> import poc
... import sys
... for attr in ['executable', 'path', 'prefix', 'base_prefix', 'exec_prefix', 'base_exec_prefix']: print(f'sys.{attr}:', getattr(sys, attr))
...
Py_GetProgramName: python
Py_GetProgramFullPath: /tmp/foo/bin/python
Py_GetPath: /home/y5/micromamba/envs/py3.13/lib/python313.zip:/home/y5/micromamba/envs/py3.13/lib/python3.13:/home/y5/micromamba/envs/py3.13/lib/python3.13/lib-dynload
Py_GetPrefix: /home/y5/micromamba/envs/py3.13
Py_GetExecPrefix: /home/y5/micromamba/envs/py3.13
sys.executable: /tmp/foo/bin/python
sys.path: ['', '/home/y5/micromamba/envs/py3.13/lib/python313.zip', '/home/y5/micromamba/envs/py3.13/lib/python3.13', '/home/y5/micromamba/envs/py3.13/lib/python3.13/lib-dynload', '/tmp/foo/lib/python3.13/site-packages']
sys.prefix: /tmp/foo
sys.base_prefix: /home/y5/micromamba/envs/py3.13
sys.exec_prefix: /tmp/foo
sys.base_exec_prefix: /home/y5/micromamba/envs/py3.13