Game World!

Join A World Of Gamers

Enter your email address:

Delivered by FeedBurner

Followers

Popular Posts

Tuesday 29 June 2021

Is Python 3 a CPython?

 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?

782

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.?

JythonIronPython 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.

  • 120
    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. – Douglas Mendizábal Jun 16 '13 at 7:54
  • 6
    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. – rahmu Jun 16 '13 at 11:59
  • 11
    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: stackoverflow.com/questions/18946662/… – Max Leske Dec 2 '14 at 14:22
  • 1
    Got excited about IronPython...until I saw that it only supports Python 2.x. – Sean Anderson Sep 21 '18 at 18:33
  • 2
    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. – cesss Aug 9 '20 at 20:11 
109

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).

  • 2
    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" – Raymond Chenon Mar 30 '17 at 11:47
  • 3
    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 ? – Suhaib Oct 1 '17 at 19:43
  • 9
    @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. – Hendy Irawan Dec 11 '17 at 3:10 
  • 1
    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 ? – t7e Oct 5 '20 at 11:17
49

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.

  • 3
    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. – rahulaga_dev Nov 2 '18 at 14:02
  • 1
    CPython (the implementation) also executes it, meaning it contains the functionality which converts byte code to machine code. – Vijay Kumar Aug 13 '20 at 4:18
32

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.

24

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 :)

9

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)

6

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.

  • 18
    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. – Martijn Pieters Feb 13 '14 at 0:05
0

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.

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

0

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 there are 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 by PVM or Python Virtual Machine

  • 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 and 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 for execution as it has dependency on them to understand the written CPython code
  • 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 then that bytecode gets interpreted by JVM or Java Virtual Machine

  • 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 while CPython has an Interpreter. PyPy aims to provide a common translation and support framework for producing implementations of dynamic languages, where there will be clean separation between language specification and implementation.

    • Compiler translates our high level source code into bytecode and to translate bytecode into machine code, some implementations have normal interpreter, some have Just-in-time compiler. To execute a loop which runs say, million times, a JIT compiler translates bytecode into machine code only once and for next iterations machine just understands the bytecode. Whereas normal Interpreter, in each iterationrepeatedly 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 :

ImplementationExecution Time (seconds)Speed Up
CPython9.25
CPython + Cython0.2144x
PyPy0.5716x

Floating Button

Button