CPython
   HOME

TheInfoList



OR:

CPython is the
reference implementation In the software development process, a reference implementation (or, less frequently, sample implementation or model implementation) is a program that implements all requirements from a corresponding specification. 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. CPython can be defined as both an interpreter and a
compiler In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
as it compiles Python code into
bytecode Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (normal ...
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 A global interpreter lock (GIL) is a mechanism used in computer-language Interpreter (computing), interpreters to synchronize the execution of Threads (computer science), threads so that only one native thread (per process) can execute basic ope ...
(GIL) such that for each CPython interpreter
process A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic. Things called a process include: Business and management * Business process, activities that produce a specific s ...
, only one thread may be processing
bytecode Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (normal ...
at a time. 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 In computing, a database is an organized collection of data or a type of data store based on the use of a database management system (DBMS), the software that interacts with end users, applications, and the database itself to capture and a ...
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 sub-interpreter in a single Python process and have been described as "threads with opt-in sharing". After several debates, a project was launched in 2023 to propose making the GIL optional from version 3.13 of Python, which was released October 7, 2024, in Python 3.13.0.


History

In 2009, a
Google Google LLC (, ) is an American multinational corporation and technology company focusing on online advertising, search engine technology, cloud computing, computer software, quantum computing, e-commerce, consumer electronics, and artificial ...
sponsored branch named Unladen Swallow was created to incorporate a just-in-time compiler into CPython. Development ended in 2011 without it being merged into the main implementation, though some of its code, such as improvements to the cPickle module, made it in. 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. 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. In 2024, an experimental Just-in-time compiler was merged into CPython’s main development branch. This early JIT sits on top of
LLVM LLVM, also called LLVM Core, is a target-independent optimizer and code generator. It can be used to develop a Compiler#Front end, frontend for any programming language and a Compiler#Back end, backend for any instruction set architecture. LLVM i ...
, aiming to speed up hot code paths. At the time of the merge, the compiler was still not included in CPython’s default build configurations and offered roughly equal performance to the SAI; one of the conditions for its full adoption was a performance increase of at least 5%. It remains disabled by default, though users can enable it to experiment and see how Python might eventually rival other JIT’d languages.


Distribution

Officially supported tier-1 platforms are
Linux Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
for 64-bit Intel using a GCC toolchain,
macOS macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
for 64-bit Intel and ARM, and
Microsoft Windows Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
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: ; Unix-like: ; Special and embedded: ; Other: PEP 11 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. The support status of Python in the table refers to support from the Python core team, and not from the distribution maintainer.


Alternatives

CPython is one of several "production-quality" Python implementations including: Jython, written in
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
for the
Java virtual machine A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally descr ...
(JVM); PyPy, written in RPython and translated into C; and IronPython, written in C# for the
Common Language Infrastructure The Common Language Infrastructure (CLI) is an open specification and technical standard originally developed by Microsoft and standardized by International Organization for Standardization, ISO/International Electrotechnical Commission, IEC (ISO/ ...
. There are also several experimental implementations.


References


Further reading

*


External links

* {{Python (programming language) Free and open source compilers Free and open source interpreters Free software programmed in C Free software programmed in Python Python (programming language) implementations Software using the Python Software Foundation License Stack-based virtual machines