The Question :
493 people think this question is useful
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?
The Question Comments :
The Answer 1
750 people think this answer is useful
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.
The Answer 2
100 people think this answer is useful
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).
The Answer 3
46 people think this answer is useful
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:
- Python: It is a language, it only states/describes how to convey/express yourself to the interpreter (the program which accepts your python code).
- Implementation: It is all about how the interpreter was written, specifically, in what language and what it ends up doing.
- 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
3 8 LOAD_FAST 0 (x)
10 POP_JUMP_IF_FALSE 20
4 12 LOAD_FAST 1 (y)
14 LOAD_FAST 0 (x)
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
6 30 LOAD_FAST 0 (x)
32 LOAD_FAST 1 (y)
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.
The Answer 4
32 people think this answer is useful
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
The second thing to realize is that ‘interpreted’ and ‘compiled’ are
properties of an implementation, not an interface.
The Answer 5
22 people think this answer is useful
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 🙂
The Answer 6
9 people think this answer is useful
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)
The Answer 7
6 people think this answer is useful
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.
The Answer 8
0 people think this answer is useful
The original, and standard, implementation of Python is usually called
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
Unless you want to script
.NET applications with Python or find the benefits
PyPy compelling, you probably want to use the standard
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
The Answer 9
0 people think this answer is useful
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