Python vs Cpython
Asked Answered
M

11

650

What's all this fuss about Python and CPython (Jython,IronPython), I don't get it:

python.org mentions that CPython is:

The "traditional" implementation of Python (nicknamed CPython)

yet another Stack Overflow question mentions that:

CPython is the default byte-code interpreter of Python, which is written in C.

Honestly I don't get what both of those explanations practically mean but what I thought was that, if I use CPython does that mean when I run a sample python code, it compiles it to C language and then executes it as if it were C code

So what exactly is CPython and how does it differ when compared with python and should I probably use CPython over Python and if so what are its advantages?

Meit answered 16/6, 2013 at 7:0 Comment(1)
Related post - Is there any difference between cpython and pythonOverlay
S
952

So what is CPython?

CPython is the original Python implementation. It is the implementation you download from Python.org. People call it CPython to distinguish it from other, later, Python implementations, and to distinguish the implementation of the language engine from the Python programming language itself.

The latter part is where your confusion comes from; you need to keep Python-the-language separate from whatever runs the Python code.

CPython happens to be implemented in C. That is just an implementation detail, really. CPython compiles your Python code into bytecode (transparently) and interprets that bytecode in a evaluation loop.

CPython is also the first to implement new features; Python-the-language development uses CPython as the base; other implementations follow.

What about Jython, etc.?

Jython, IronPython and PyPy are the current "other" implementations of the Python programming language; these are implemented in Java, C# and RPython (a subset of Python), respectively. Jython compiles your Python code to Java bytecode, so your Python code can run on the JVM. IronPython lets you run Python on the Microsoft CLR. And PyPy, being implemented in (a subset of) Python, lets you run Python code faster than CPython, which rightly should blow your mind. :-)

Actually compiling to C

So CPython does not translate your Python code to C by itself. Instead, it runs an interpreter loop. There is a project that does translate Python-ish code to C, and that is called Cython. Cython adds a few extensions to the Python language, and lets you compile your code to C extensions, code that plugs into the CPython interpreter.

Shirl answered 16/6, 2013 at 7:2 Comment(9)
I think it's worth mentioning that, in theory, a python script could be run using any of the implementations, and the results of running the script should be the same.Lenhart
As a matter of facts, and depending on the project you're working on, it may be a good idea to test and profile your Python code on several implementations. Having worked on Java+Jython projects before, you can run into many surprises because devs haven't tested their libs enough on this platform.Joses
Saying that "PyPy is faster than CPython" is a bit dangerous I'd say. There's a very good answer to precisely that question here: #18947162Succeed
Got excited about IronPython...until I saw that it only supports Python 2.x.Avaunt
@SeanAnderson: Same goes for Jython (until 2015 the official release was only 2.5 compatible, making it nine years out of date; they finally got a 2.7 release out the door in 2015, but still no sign of a 3.x release).Bolte
I really think there's a problem with this naming, because when you go to python.org and you download Python, you are told you are downloading Python (and even the file package is named Python), but however you are downloading CPython rather than Python (because Python "doesn't exist", it's the language, not an implementation). I don't get why did they need to do this so complicated. If it must be clear that the reference implementation is CPython, then call it CPython, not Python. I had to search for it just to understand what's this about.Jacintajacinth
@cesss: the reference implementation is often referenced as CPython. It is not an official name. That's why I used the phrase: People call it CPython to distinguish it from other, later, Python implementations.Shirl
PyPy, being implemented in (a subset of) Python. Is that python's implementation is CPython ?Niccolite
@Rilwan: PyPy defined a subset called RPython, which is then translated by their toolchain into a source code for a different, compiled language and then compiled to machine code. See the RPython architecture overview.Shirl
Z
134

You need to distinguish between a language and an implementation. Python is a language,

According to Wikipedia, "A programming language is a notation for writing programs, which are specifications of a computation or algorithm". This means that it's simply the rules and syntax for writing code. Separately we have a programming language implementation which in most cases, is the actual interpreter or compiler.

Python is a language. CPython is the implementation of Python in C. Jython is the implementation in Java, and so on.

To sum up: You are already using CPython (if you downloaded from here).

Zinck answered 16/6, 2013 at 7:7 Comment(4)
You should read Martijn Pieters 's post "CPython does not translate your Python code to C by itself. It instead runs a interpreter loop. There is a project that does translate Python-ish code to C, and that is called Cython"Juta
why there is so much imphasization about Cpython. i.e: we don't hear Cc++ or CJava or even CSwift. Or am I missing something ?Hierology
@Suhaib: they do, in Java world people have e.g. HotSpot, OpenJDK, IBM J9 JDK, Azul. They usually are more precise, i.e. you don't really see "install Java" but rather "install a compatible Java 8 JDK, for example Oracle JDK 8". In JavaScript world you have node.js, V8, etc. You "install node.js", not "install JavaScript", right? But in Python world it's pretty common to just say "install Python 3.6" and done, referring to a language spec, not a specific runtime.Ochs
CPython is the implementation of Python in C. C is also a programming language and is a specification of a computation or algorithm. So I don't get it what C implementation do they use in CPython ?Informality
S
70

Even I had the same problem understanding how are CPython, JPython, IronPython, PyPy are different from each other.

So, I am willing to clear three things before I begin to explain:

  1. Python: It is a language, it only states/describes how to convey/express yourself to the interpreter (the program which accepts your python code).
  2. Implementation: It is all about how the interpreter was written, specifically, in what language and what it ends up doing.
  3. Bytecode: It is the code that is processed by a program, usually referred to as a virtual machine, rather than by the "real" computer machine, the hardware processor.

CPython is the implementation, which was written in C language. It ends up producing bytecode (stack-machine based instruction set) which is Python specific and then executes it. The reason to convert Python code to a bytecode is because it's easier to implement an interpreter if it looks like machine instructions. But, it isn't necessary to produce some bytecode prior to execution of the Python code (but CPython does produce).

If you want to look at CPython's bytecode then you can. Here's how you can:

>>> def f(x, y):                # line 1
...    print("Hello")           # line 2
...    if x:                    # line 3
...       y += x                # line 4
...    print(x, y)              # line 5
...    return x+y               # line 6
...                             # line 7
>>> import dis                  # line 8
>>> dis.dis(f)                  # line 9
  2           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('Hello')
              4 CALL_FUNCTION            1
              6 POP_TOP

  3           8 LOAD_FAST                0 (x)
             10 POP_JUMP_IF_FALSE       20

  4          12 LOAD_FAST                1 (y)
             14 LOAD_FAST                0 (x)
             16 INPLACE_ADD
             18 STORE_FAST               1 (y)

  5     >>   20 LOAD_GLOBAL              0 (print)
             22 LOAD_FAST                0 (x)
             24 LOAD_FAST                1 (y)
             26 CALL_FUNCTION            2
             28 POP_TOP

  6          30 LOAD_FAST                0 (x)
             32 LOAD_FAST                1 (y)
             34 BINARY_ADD
36 RETURN_VALUE

Now, let's have a look at the above code. Lines 1 to 6 are a function definition. In line 8, we import the 'dis' module which can be used to view the intermediate Python bytecode (or you can say, disassembler for Python bytecode) that is generated by CPython (interpreter).

NOTE: I got the link to this code from #python IRC channel: https://gist.github.com/nedbat/e89fa710db0edfb9057dc8d18d979f9c

And then, there is Jython, which is written in Java and ends up producing Java byte code. The Java byte code runs on Java Runtime Environment, which is an implementation of Java Virtual Machine (JVM). If this is confusing then I suspect that you have no clue how Java works. In layman terms, Java (the language, not the compiler) code is taken by the Java compiler and outputs a file (which is Java byte code) that can be run only using a JRE. This is done so that, once the Java code is compiled then it can be ported to other machines in Java byte code format, which can be only run by JRE. If this is still confusing then you may want to have a look at this web page.

Here, you may ask if the CPython's bytecode is portable like Jython, I suspect not. The bytecode produced in CPython implementation was specific to that interpreter for making it easy for further execution of code (I also suspect that, such intermediate bytecode production, just for the ease the of processing is done in many other interpreters).

So, in Jython, when you compile your Python code, you end up with Java byte code, which can be run on a JVM.

Similarly, IronPython (written in C# language) compiles down your Python code to Common Language Runtime (CLR), which is a similar technology as compared to JVM, developed by Microsoft.

Scavenge answered 17/11, 2016 at 23:11 Comment(2)
thx for detailed explanation !! This means CPython takes care of converting Python code to Byte Code and also interpret byte code to machine code ? In nut shell CPython is Compiler (for Python to Byte Code) and Python Virtual Machine (for Byte code to Machine code) ? Comparing same to .Net, there is C# Compiler to Convert C# to MSIL and CLR to convert from MSIL to Machine code.Rinker
CPython (the implementation) also executes it, meaning it contains the functionality which converts byte code to machine code.Scavenge
Z
53

As python is open source that's why we can customize python as per our requirements. After customization, we can name that version as we want. That's why multiple flavours of python are available. Each flavour is a customized version of python to fulfill a special requirement. It is similar to the fact that there are multiple flavours of UNIX like, Ubuntu, Linux, RedHat Linux etc. Below are some of the flavours of python :

CPython

Default implementation of python programming language which we download from python.org, provided by python software foundation. It is written in C and python. It does not allow us to write any C code, only python code are allowed. CPython can be called as both an interpreter and a compiler as here our python code first gets compiled to python bytecode then the bytecode gets interpreted into platform-specific operations by PVM or Python Virtual Machine. Keep in mind interpreters have language syntaxes predefined that's why it does not need to translate into low level machine code. Here Interpreter just executes bytecode on the fly during runtime and results in platform-specific operations.

code execution in cpython

Old versions of JavaScript, Ruby, Php were fully interpreted languages as their interpreters would directly translate each line source code into platform-specific operations, no bytecode was involved. Bytecode is there in Java, Python, C++ (.net), C# to decouple the language from execution environment, i.e. for portability, write once, run anywhere. Since 2008, Google Chrome's V8 JavaScript engine has come up with Just-In-Time Compiler for JavaScript. It executes JavaScript code line-by-line like an interpreter to reduce start up time, but if encounters with a hot section with repeatedly executed line of code then optimizes that code using baseline or optimizing compiler.

Cython

Cython is a programming language which is a superset of python and C. It is written in C and python. It is designed to give C-like performance with python syntax and optional C-syntax. Cython is a compiled language as it generates C code and gets compiled by C compiler. We can write similar code in Cython as in default python or CPython, the differences are :

  • Cython allows us to write optional additional C code and,
  • In Cython, our python code gets translated into C code internally so that it can get compiled by C compiler. Although Cython results in considerably faster execution, but falls short of original C language execution. This is because Cython has to make calls to the CPython interpreter and CPython standard libraries to understand the written CPython code

enter image description here

JPython / Jython

Java implementation of python programming language. It is written in Java and python. Here our python code first gets compiled to Java bytecode and then that bytecode gets interpreted to platform-specific operations by JVM or Java Virtual Machine. This is similar to how Java code gets executed : Java code first gets compiled to intermediate bytecode then that bytecode gets interpreted to platform-specific operations by JVM

PyPy

RPython implementation of python programming language. It is written in a restricted subset of python called Restricted Python (RPython). PyPy runs faster than CPython because to interpret bytecode, PyPy has a Just-in-time Compiler (Interpreter + Compiler) while CPython has an Interpreter. So JIT Compiler in PyPy can execute Python bytecode line-by-line like an Interpreter to reduce start up time, but if encounters with a hot section with repeatedly executed line of code then optimizes that code using Baseline or Optimizing Compiler.

pypy code execution

JIT Compiler in a Nutshell: Compiler in Python translates our high level source code into bytecode and to execute bytecode, some implementations have normal Interpreter, some have Just-in-time Compiler. To execute a loop which runs say, million times i.e. a very hot code, initially Interpreter will run it for some time and Monitor of JIT Compiler will watch that code. Then when it gets repeated some times i.e. the code becomes warm* then JIT compiler will send that code to Baseline Compiler which will make some assumptions on variable types etc. based on data gathered by Monitor while watching the code. From next iterations if assumptions turn out to be valid, then no need to retranslate bytecode into machine code, i.e. steps can be skipped for faster execution. If the code repeats a lot of times i.e. code becomes very hot then JIT compiler will send that code to Optimizing Compiler which will make more assumptions and will skip more steps for very fast execution.

JIT Compiler Drawbacks: initial slower execution when the code gets analysed, and if assumptions turn out to be false then optimized compiled code gets thrown out i.e. Deoptimization or Bailing out happens which can make code execution slower, although JIT compilers has limit for optimization/deoptimization cycle. After certain number of deoptimization happens, JIT Compiler just does not try to optimize anymore. Whereas normal Interpreter, in each iteration, repeatedly translates bytecode into machine code thereby taking more time to complete a loop which runs say, million times

IronPython

C# implementation of python, targeting the .NET framework

Ruby Python

works with Ruby platform

Anaconda Python

Distribution of python and R programming languages for scientific computing like, data science, machine learning, artificial intelligence, deep learning, handling large volume of data etc. Numerous number of libraries like, scikit-learn, tensorflow, pytorch, numba, pandas, jupyter, numpy, matplotlib etc. are available with this package

Stackless

Python for Concurrency

To test speed of each implementation, we write a program to call integrate_f 500 times using an N value of 50,000, and record the execution time over several runs. Below table shows the benchmark results :

Implementation Execution Time (seconds) Speed Up
CPython 9.25
CPython + Cython 0.21 44x
PyPy 0.57 16x
Zales answered 10/6, 2021 at 5:32 Comment(0)
L
37

This article thoroughly explains the difference between different implementations of Python. Like the article puts it:

The first thing to realize is that ‘Python’ is an interface. There’s a specification of what Python should do and how it should behave (as with any interface). And there are multiple implementations (as with any interface).

The second thing to realize is that ‘interpreted’ and ‘compiled’ are properties of an implementation, not an interface.

Lacuna answered 20/2, 2014 at 17:25 Comment(0)
G
35

Python is a language: a set of rules that can be used to write programs. There are several implementaions of this language.

No matter what implementation you take, they do pretty much the same thing: take the text of your program and interpret it, executing its instructions. None of them compile your code into C or any other language.

CPython is the original implementation, written in C. (The "C" part in "CPython" refers to the language that was used to write Python interpreter itself.)

Jython is the same language (Python), but implemented using Java.

IronPython interpreter was written in C#.

There's also PyPy - a Python interpreter written in Python. Make your pick :)

Georgiana answered 16/6, 2013 at 7:6 Comment(0)
R
13

implementation means what language was used to implement Python and not how python Code would be implemented. The advantage of using CPython is the availability of C Run-time as well as easy integration with C/C++.

So CPython was originally implemented using C. There were other forks to the original implementation which enabled Python to lever-edge Java (JYthon) or .NET Runtime (IronPython).

Based on which Implementation you use, library availability might vary, for example Ctypes is not available in Jython, so any library which uses ctypes would not work in Jython. Similarly, if you want to use a Java Class, you cannot directly do so from CPython. You either need a glue (JEPP) or need to use Jython (The Java Implementation of Python)

Rutabaga answered 16/6, 2013 at 7:5 Comment(0)
I
8

You should know that CPython doesn't really support multithreading (it does, but not optimal) because of the Global Interpreter Lock. It also has no Optimisation mechanisms for recursion, and has many other limitations that other implementations and libraries try to fill.

You should take a look at this page on the python wiki.

Look at the code snippets on this page, it'll give you a good idea of what an interpreter is.

Indene answered 1/1, 2014 at 21:11 Comment(1)
CPython supports multithreading, but the GIL makes it hard to take advantage of multiple cores or CPUs. That's not quite the same thing as not supporting multithreading at all.Shirl
S
2

The original, and standard, implementation of Python is usually called CPython when you want to contrast it with the other options (and just plain “Python” otherwise). This name comes from the fact that it is coded in portable ANSI C language code. This is the Python that you fetch from http://www.python.org, get with the ActivePython and Enthought distributions, and have automatically on most Linux and Mac OS X machines. If you’ve found a preinstalled version of Python on your machine, it’s probably CPython, unless your company or organization is using Python in more specialized ways.

Unless you want to script Java or .NET applications with Python or find the benefits of Stackless or PyPy compelling, you probably want to use the standard CPython system. Because it is the reference implementation of the language, it tends to run the fastest, be the most complete, and be more up-to-date and robust than the alternative systems.

Serosa answered 4/1, 2018 at 6:15 Comment(1)
I don't mean to be rude, but CPython absolutely does not run the fastest.Spangler
W
0

A programming language implementation is a system for executing computer programs.

There are two general approaches to programming language implementation:

  • Interpretation: An interpreter takes as input a program in some language, and performs the actions written in that language on some machine.
  • Compilation: A compiler takes as input a program in some language, and translates that program into some other language, which may serve as input to another interpreter or another compiler.

Python is an interpreted high-level programming language created by Guido van Rossum in 1991.

CPython is reference version of the Python computing language, which is written in C created by Guido van Rossum too.

Other list of Python Implementations

Source

Washhouse answered 24/8, 2018 at 3:8 Comment(0)
P
0

Cpython is the default implementation of Python and the one which we get onto our system when we download Python from its official website.

Cpython compiles the python source code file with .py extension into an intermediate bytecode which is usually given the .pyc extension, and gets executed by the Cpython Virtual Machine. This implementation of Python provides maximum compatibility with the Python packages and C extension modules.

There are many other Python implementations such as IronPython, Jython, PyPy, CPython, Stackless Python and many more.

Potable answered 12/9, 2022 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.