CPython exposes its version number in the following macros. Note that these correspond to the version code is built with. See Py_Version for the version used at run time.
See C API Stability for a discussion of API and ABI stability across versions.
PY_MAJOR_VERSION The 3 in 3.4.1a2.
PY_MINOR_VERSION The 4 in 3.4.1a2.
PY_MICRO_VERSION The 1 in 3.4.1a2.
PY_RELEASE_LEVEL The a in 3.4.1a2. This can be 0xA for alpha, 0xB for beta, 0xC for release candidate or 0xF for final.
PY_RELEASE_SERIAL The 2 in 3.4.1a2. Zero for final releases.
PY_VERSION_HEX The Python version number encoded in a single integer. See Py_PACK_FULL_VERSION() for the encoding details.
Use this for numeric comparisons, for example, #if PY_VERSION_HEX >= ....
These macros are defined in Include/patchlevel.h.
const unsigned long Py_Version The Python runtime version number encoded in a single constant integer. See Py_PACK_FULL_VERSION() for the encoding details. This contains the Python version used at run time.
Use this for numeric comparisons, for example, if (Py_Version >= ...).
Added in version 3.11.
uint32_t Py_PACK_FULL_VERSION(int major, int minor, int micro, int release_level, int release_serial) Return the given version, encoded as a single 32-bit integer with the following structure:
Argument | No. of bits | Bit mask | Bit shift | Example values | |
|---|---|---|---|---|---|
|
| ||||
major | 8 |
| 24 |
|
|
minor | 8 |
| 16 |
|
|
micro | 8 |
| 8 |
|
|
release_level | 4 |
| 4 |
|
|
release_serial | 4 |
| 0 |
|
|
For example:
Version |
| Encoded version |
|---|---|---|
|
|
|
|
|
|
Out-of range bits in the arguments are ignored. That is, the macro can be defined as:
#ifndef Py_PACK_FULL_VERSION #define Py_PACK_FULL_VERSION(X, Y, Z, LEVEL, SERIAL) ( \ (((X) & 0xff) << 24) | \ (((Y) & 0xff) << 16) | \ (((Z) & 0xff) << 8) | \ (((LEVEL) & 0xf) << 4) | \ (((SERIAL) & 0xf) << 0)) #endif
Py_PACK_FULL_VERSION is primarily a macro, intended for use in #if directives, but it is also available as an exported function.
Added in version 3.14.
uint32_t Py_PACK_VERSION(int major, int minor) Equivalent to Py_PACK_FULL_VERSION(major, minor, 0, 0, 0). The result does not correspond to any Python release, but is useful in numeric comparisons.
Added in version 3.14.
© 2001–2025 Python Software Foundation
Licensed under the PSF License.
https://docs.python.org/3.14/c-api/apiabiversion.html