CPython

Last updated
CPython
Original author(s) Guido van Rossum
Developer(s) Python core developers and the Python community, supported by the Python Software Foundation
Initial release26 January 1994;31 years ago (1994-01-26)
Stable release
3.13.1 [1]   OOjs UI icon edit-ltr-progressive.svg / 3 December 2024;2 months ago (3 December 2024)
Repository https://github.com/python/cpython
Written in C, Python
Platform 42 platforms; see § Distribution
Available in English
Type Python Programming Language Interpreter
License Python Software Foundation License
Website www.python.org

CPython is the reference implementation of the Python programming language. Written in C and Python, CPython is the default and most widely used implementation of the Python language.

Contents

CPython can be defined as both an interpreter and a compiler as it compiles Python code into bytecode before interpreting it. It has a foreign function interface with several languages, including C, in which one must explicitly write bindings in a language other than Python.

Design

A particular feature of CPython is that it makes use of a global interpreter lock (GIL) such that for each CPython interpreter process, only one thread may be processing bytecode at a time. [2] This does not mean that there is no point in multithreading; the most common multithreading scenario is where threads are mostly waiting on external processes to complete.

This can happen when multiple threads are servicing separate clients. One thread may be waiting for a client to reply, and another may be waiting for a database query to execute, while the third thread is actually processing Python code.

However, the GIL does mean that CPython is not suitable for processes that implement CPU-intensive algorithms in Python code that could potentially be distributed across multiple cores.

In real-world applications, situations where the GIL is a significant bottleneck are quite rare. This is because Python is an inherently slow language and is generally not used for CPU-intensive or time-sensitive operations. Python is typically used at the top level and calls functions in libraries to perform specialized tasks. These libraries are generally not written in Python, and Python code in another thread can be executed while a call to one of these underlying processes takes place. The non-Python library being called to perform the CPU-intensive task is not subject to the GIL and may concurrently execute many threads on multiple processors without restriction.

Concurrency of Python code can only be achieved with separate CPython interpreter processes managed by a multitasking operating system. This complicates communication between concurrent Python processes, though the multiprocessing module mitigates this somewhat; it means that applications that really can benefit from concurrent Python-code execution can be implemented with limited overhead.

The presence of the GIL simplifies the implementation of CPython, and makes it easier to implement multi-threaded applications that do not benefit from concurrent Python code execution. However, without a GIL, multiprocessing apps must make sure all common code is thread safe.

Although many proposals have been made to eliminate the GIL, the general consensus has been that in most cases, the advantages of the GIL outweigh the disadvantages; in the few cases where the GIL is a bottleneck, the application should be built around the multiprocessing structure. To help allow more parallelism, an improvement was released in October 2023 to allow a separate GIL per subinterpreter in a single Python process and have been described as "threads with opt-in sharing". [3] [4]

After several debates, a project was launched in 2023 to propose making the GIL optional from version 3.13 of Python, [5] which is scheduled for release in October 2024. [6]

History

In 2009, a Google sponsored branch named Unladen Swallow was created to incorporate a just-in-time compiler into CPython. [7] [8] Development ended in 2011 without it being merged into the main implementation, [9] though some of its code, such as improvements to the cPickle module, made it in. [10] [7]

In 2021, a "specializing adaptive interpreter" was proposed, which was measured to improve performance by 10-60% by specializing commonly executed instructions displaying apparent type stability into faster, type specific instructions, and which could de-specialize instructions when necessary. [11] The SAI was first included in Python 3.11, which was measured to be 25% faster on average than Python 3.10 by the "pyperformance" benchmark suite. [12]

In 2024, an experimental Just-in-time compiler was merged into CPython's main development branch. At the time of the merge, the compiler was still not included in CPython's default build configurations, and it offered roughly equal performance to the SAI; one of the conditions for its full adoption was a performance increase of at least 5%. [13] As of February 2025, the compiler is still experimental.

Distribution

Officially supported tier-1 platforms are Linux for 64-bit Intel using a GCC toolchain, macOS for 64-bit Intel and ARM, and Microsoft Windows for 32- and 64-bit Intel. Official tier-2 support exists for Linux for 64-bit ARM, wasm32 (Web Assembly) with WASI runtime support, and Linux for 64-bit Intel using a clang toolchain. Official supported tier-3 systems include 64-bit ARM Windows, 64-bit iOS, Raspberry Pi OS (Linux for armv7 with hard float), Linux for 64-bit PowerPC in little-endian mode, and Linux for s390x.

More platforms have working implementations, including: [14]

Unix-like
Special and embedded
Other

PEP 11 [15] lists platforms which are not supported in CPython by the Python Software Foundation. These platforms can still be supported by external ports. These ports include:

External ports not integrated to Python Software Foundation's official version of CPython, with links to its main development site, often include additional modules for platform-specific functionalities, like graphics and sound API for PSP and SMS and camera API for S60. These ports include:

Enterprise Linux

These Python versions are distributed with currently-supported enterprise Linux distributions. [24] The support status of Python in the table refers to support from the Python core team, and not from the distribution maintainer.

Enterprise Linux
Distribution versionDistribution end-of-life Python version
Ubuntu 22.04 LTS (Jammy Jellyfish)3.10
Ubuntu 20.04 LTS (Focal Fossa)2030-04 [25] [ needs update ] [26] class="templateVersion co swatch-maintained" style="color: var(--color-base, #202122); " title="Old version, still maintained" data-sort-value="3.8" | Old version, still maintained: 3.8 [27] [ needs update ]
Ubuntu 18.04 LTS (Bionic Beaver)2028-04 [28] class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="2.7" | Old version, not maintained: 2.7 [29] class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="3.6" | Old version, not maintained: 3.6 [30]
Ubuntu 16.04 LTS (Xenial Xerus)2021-04-30 [31] [ needs update ]class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="2.7" | Old version, not maintained: 2.7 [29] class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="3.5" | Old version, not maintained: 3.5 [30]
Debian 122028-06 [32] class="templateVersion co swatch-maintained" style="color: var(--color-base, #202122); " title="Old version, still maintained" data-sort-value="3.11" | Old version, still maintained: 3.11 [32]
Debian 112026-06 [33] class="templateVersion co swatch-maintained" style="color: var(--color-base, #202122); " title="Old version, still maintained" data-sort-value="3.9" | Old version, still maintained: 3.9 [33]
Debian 102024-06 [34] [ needs update ]class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="2.7" | Old version, not maintained: 2.7 [35] class="templateVersion co swatch-maintained" style="color: var(--color-base, #202122); " title="Old version, still maintained" data-sort-value="3.7" | Old version, still maintained: 3.7 [36] [ needs update ]
Debian 92022-06-30 [37] [ needs update ]class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="2.7" | Old version, not maintained: 2.7 [38] class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="3.5" | Old version, not maintained: 3.5 [39]
Red Hat Enterprise Linux 82029class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="2.7" | Old version, not maintained: 2.7 [40] class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="3.6" | Old version, not maintained: 3.6
Red Hat Enterprise Linux 72024-11-30 [41] [ needs update ]class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="2.7" | Old version, not maintained: 2.7 [42]
CentOS 82029-05-31class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="2.7" | Old version, not maintained: 2.7class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="3.6" | Old version, not maintained: 3.6
CentOS 72024-06-30[ needs update ]class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="2.7" | Old version, not maintained: 2.7 [43]
SUSE Linux Enterprise Server 152031-07-31class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="2.7" | Old version, not maintained: 2.7 [44] class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="3.6" | Old version, not maintained: 3.6
SUSE Linux Enterprise Server 122027-10-31class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="2.7" | Old version, not maintained: 2.7 [45]
SUSE Linux Enterprise Server 112022-03-31[ needs update ]class="templateVersion co swatch-unsupported" style="color: var(--color-base, #202122); " title="Old version, not maintained" data-sort-value="2.7" | Old version, not maintained: 2.7 [45]
Legend:
Old version, not maintained
Old version, still maintained
Latest version

Alternatives

CPython is one of several "production-quality" Python implementations including: Jython, written in Java for the Java virtual machine (JVM); PyPy, written in RPython and translated into C; and IronPython, written in C# for the Common Language Infrastructure. There are also several experimental implementations. [46]

References

  1. "Release 3.13.1". 3 December 2024. Retrieved 23 December 2024.
  2. "Initialization, Finalization, and Threads". Python v3.8.3 documentation. Retrieved 2020-06-04.
  3. Jake Edge (August 15, 2023). "A per-interpreter GIL". LWN. Retrieved 2024-01-13.
  4. "PEP 684 – A Per-Interpreter GIL | peps.python.org" . Retrieved 2024-01-13.
  5. "PEP 703 – Making the Global Interpreter Lock Optional in CPython | peps.python.org". peps.python.org. Retrieved 2023-09-17.
  6. "PEP 719 – Python 3.13 Release Schedule | peps.python.org". peps.python.org. Retrieved 2023-09-17.
  7. 1 2 Winter, Collin; Yasskin, Jeffrey; Kleckner, Reid (2010-03-17). "PEP 3146 - Merging Unladen Swallow into CPython". Python.org.
  8. "Unladen Swallow 2009Q1". unladen-swallow, A faster implementation of Python. Retrieved 19 October 2012.
  9. Kleckner, Reid (26 March 2011). "Unladen Swallow Retrospective". QINSB is not a Software Blog (qinsb.blogspot.com).
  10. "Issue 9410: Add Unladen Swallow's optimizations to Python 3's pickle. - Python tracker". bugs.python.org. Retrieved 2019-08-08.
  11. Mark Shannon (April 13, 2021). "PEP 659 – Specializing Adaptive Interpreter".
  12. Pablo Galindo Salgado. "What's new in Python 3.11 § PEP 659: Specializing Adaptive Interpreter".
  13. Brandt Bucher; Savannah Ostrowski (April 11, 2024). "PEP 744 {{subst:endash}} JIT Compilation" . Retrieved February 5, 2025.
  14. "PythonImplementations" . Retrieved 19 July 2012.
  15. "PEP 11 -- Removing support for little used platforms". Python.org. Retrieved 2019-08-08.
  16. "Irix still supported?". 14 February 2009.
  17. "AmigaPython Home Page". www.monkeyhouse.eclipse.co.uk. Retrieved Feb 20, 2025.
  18. "I | Series | Python". I | Series | Python. Retrieved Feb 20, 2025.
  19. "PythonD 32bit Python for DOS and Windows". www.caddit.net. Retrieved Feb 20, 2025.
  20. http://yellowblue.free.fr/yiki/doku.php/en:dev:python:start Python 2 and 3
  21. "Google Code Archive - Long-term storage for Google Code Project Hosting". code.google.com. Retrieved Feb 20, 2025.
  22. "Python Windows CE port". SourceForge. Jan 22, 2018. Retrieved Feb 20, 2025.
  23. "Python". VSI. Retrieved 2021-08-31.
  24. "Support Life Cycles for Enterprise Linux Distributions". Archived from the original on 2022-08-30. Retrieved 2017-10-15.
  25. "Ubuntu release cycle". Ubuntu. Retrieved 2021-01-18.
  26. "With Python 2 EOL'ed, Ubuntu 20.04 LTS Moves Along With Its Python 2 Removal - Phoronix". phoronix.com. Retrieved 2020-04-01.
  27. "Binary package "python3" in ubuntu focal". Launchpad.net.
  28. "Ubuntu 18.04 extended to 2028". ServerWatch.com. 2018-11-15. Retrieved 2019-09-09.
  29. 1 2 "python-defaults package: Ubuntu". Canonical Ltd. 2018-06-08. Retrieved 2018-06-08.
  30. 1 2 "python3-defaults package: Ubuntu". Canonical Ltd. 2018-06-08. Retrieved 2018-06-08.
  31. Science, Carnegie Mellon University School of Computer. "Ubuntu 16.04 - End of Life in 2021 - SCS Computing Facilities - Carnegie Mellon University". computing.cs.cmu.edu. Retrieved 2021-02-15.
  32. 1 2 "Debian 12 bookworm released". debian.org.
  33. 1 2 "Debian -- News -- Debian 11 "bullseye" released". debian.org. Archived from the original on 2021-08-14. Retrieved 2022-01-04.
  34. "LTS - Debian Wiki". wiki.debian.org. Retrieved 2021-02-15.
  35. "Debian -- Details of package python in buster". packages.debian.org. Retrieved 2019-09-13.
  36. "Debian -- News -- Debian 10 "buster" released". debian.org. Retrieved 2019-08-09.
  37. "Debian -- News -- Debian 8 Long Term Support reaching end-of-life". debian.org. Retrieved 2021-02-15.
  38. "DistroWatch.com: Debian". DistroWatch.com. 2017-10-15. Retrieved 2017-10-15.
  39. "Debian -- Details of package python3 in stretch" . Retrieved 2017-12-19.
  40. "Python in RHEL 8". Red Hat Developer Blog. 2018-11-14. Archived from the original on 2019-05-10. Retrieved 2019-05-10.
  41. "Red Hat Enterprise Linux Life Cycle". Red Hat Customer Portal. Retrieved 2020-04-01.
  42. "DistroWatch.com: Red Hat Enterprise Linux". DistroWatch.com. 2017-09-07. Retrieved 2017-10-15.
  43. "DistroWatch.com: CentOS". DistroWatch.com. 2017-09-14. Retrieved 2017-10-15.
  44. "Release Notes | SUSE Linux Enterprise Desktop/SUSE Linux Enterprise Workstation Extension 15 GA". suse.com. Retrieved 2019-08-08.
  45. 1 2 "DistroWatch.com: openSUSE". DistroWatch.com. 2017-10-14. Retrieved 2017-10-15.
  46. Martelli, Alex (2006). Python in a Nutshell (2nd ed.). O'Reilly. pp. 5–7. ISBN   978-0-596-10046-9.

Further reading