Chapter 1. Picking an Interpreter
The State of Python 2 Versus Python 3
When choosing a Python interpreter, one looming question is always present: âShould I choose Python 2 or Python 3?â The answer is not as obvious as one might think (although 3 is becoming more compelling every day).
Here is the state of things:
-
Python 2.7 has been the standard for a long time.
-
Python 3 introduced major changes to the language, which some developers are unhappy with.1
-
Python 2.7 will receive necessary security updates until 2020.
-
Python 3 is continually evolving, like Python 2 did in years past.
You can now see why this is not such an easy decision.
Recommendations
The way we see it, a truly hoopy frood2 would use Python 3. But if you can only use Python 2, at least youâre still using Python. These are our recommendations:
- Use Python 3 ifâ¦
-
-
You love Python 3.
-
You donât know which one to use.
-
You embrace change.
-
- Use Python 2 ifâ¦
-
-
You love Python 2 and are saddened by the future being Python 3.
-
The stability requirements of your software would be impacted.3
-
Software that you depend on requires it.
-
Soâ¦3?
If youâre choosing a Python interpreter to use, and arenât opinionated, then use the newest Python 3.xâevery version brings new and improved standard library modules, security, and bug fixes. Progress is progress. So only use Python 2 if you have a strong reason to, such as a Python 2âexclusive library that has no adequate Python 3âready alternative, a need for a specific implementation (see âImplementationsâ), or you (like some of us) love and are inspired by Python 2.
Check out Can I Use Python 3? to see whether any Python projects youâre depending on will block adoption of Python 3.
For further reading, try Python2orPython3, which lays out some of the reasoning behind a backward-incompatible break in the language specification, and links to detailed specifications of the differences.
If youâre a beginner, there are far more important things to worry about than cross-compatibility between all of the Python versions. Just get something working for the system youâve got, and cross this bridge later.
Implementations
When people speak of Python, they often mean not just the language but also the CPython implementation. Python is actually a specification for a language that can be implemented in many different ways.
The different implementations may be for compatibility with other libraries, or maybe for a little speed. Pure Python libraries should work regardless of your Python implementation, but those built on C (like NumPy) wonât. This section provides a quick rundown on the most popular implementations.
Note
This guide presumes youâre working with the standard CPython implementation of Python 3, although weâll frequently add notes when relevant for Python 2.
CPython
CPython is the reference implementation4 of Python, written in C. It compiles Python code to intermediate bytecode which is then interpreted by a virtual machine. CPython provides the highest level of compatibility with Python packages and C extension modules.5
If you are writing open source Python code and want to reach the widest possible audience, use CPython. To use packages that rely on C extensions to function, CPython is your only implementation option.
All versions of the Python language are implemented in C because CPython is the reference implementation.
Stackless
Stackless Python is regular CPython (so it should work with all of the libraries that CPython can use), but with a patch that decouples the Python interpreter from the call stack, making it possible to change the order of execution of code. Stackless introduces the contepts of tasklets, which can wrap functions and turn them into âmicro-threadsâ that can be serialized to disk for future execution and scheduled, by default in round-robin execution.
The greenlet library implements this same stack-switching functionality for CPython users. Much of the functionality has also been implemented in PyPy.
PyPy
PyPy is a Python interpreter implemented in a restricted statically typed subset of the Python language called RPython, making certain kinds of optimization possible. The interpreter features a just-in-time compiler and supports multiple backends, such as C, Common Intermediate Language (CIL), and Java Virtual Machine (JVM) bytecode.
PyPy aims for maximum compatibility with the reference CPython implementation while improving performance. If you are looking to increase performance of your Python code, itâs worth giving PyPy a try. On a suite of benchmarks, itâs currently over five times faster than CPython.
It supports Python 2.7, and PyPy3 targets Python 3. Both versions are available from the PyPy download page.
Jython
Jython is a Python interpreter implementation that compiles Python code to Java bytecode which is then executed by the JVM. Additionally, it is able to import and use any Java class like a Python module.
If you need to interface with an existing Java code base or have other reasons for needing to write Python code for the JVM, Jython is the best choice.
Jython currently supports up to Python 2.7.
IronPython
IronPython is an implementation of Python for the .NET framework. It can use both Python and .NET framework libraries, and can also expose Python code to other languages in the .NET framework.
Python Tools for Visual Studio integrates IronPython directly into the Visual Studio development environment, making it an ideal choice for Windows developers.
IronPython supports Python2.7.
PythonNet
Python for .NET is a package that provides near seamless integration of a natively installed Python installation with the .NET Common Language Runtime (CLR). This is the inverse approach to that taken by IronPython, meaning PythonNet and IronPython complement rather than compete with each other.
In conjunction with Mono, PythonNet enables native Python installations on non-Windows operating systems, such as OSÂ X and Linux, to operate within the .NET framework. It can be run in addition to IronPython without conflict.
PythonNet supports from Python 2.3 up to Python 2.7; the installation instructions are on the PythonNet readme page.
Skulpt
Skulpt is a JavaScript implementation of Python. It has not ported all of the CPython standard library; the library has the modules math, random, turtle, image, unittest, and parts of time, urllib, DOM, and re. It is intended for use in teaching. There is also a way to add your own modules.
Notable examples of its use are Interactive Python and CodeSkulptor.
Skulpt supports most of Python 2.7 and Python 3.3. See the Skulpt GitHub page for details.
MicroPython
MicroPython is an implementation of Python 3 optimized to run on a microcontroller; it supports 32-bit ARM processors with the Thumb v2 instruction set, such as the Cortex-M range used in low-cost microcontrollers. It includes these modules from Pythonâs Standard Library, plus a few MicroPython-specific libraries for board details, memory information, network access, and a modified version of the ctypes optimized for smaller size. It is not the same as the Raspberry Pi, which has a Debian or other C-based operating system, with Python installed. The pyboard actually uses MicroPython as its operating system.
Note
From here on out, weâre using CPython on a Unix-like system, on OSÂ X, or on a Windows system.
On to installationâgrab your towel!
1 If you donât do much low-level networking programming, the change was barely noticeable outside of the print
statement becoming a function. Otherwise, âunhappy withâ is kind of a polite understatementâdevelopers responsible for large, popular web, socket, or networking libraries that deal with unicode and byte strings had (or still have) extensive changes to make. Details about the change, direct from the first introduction of Python 3 to the world, start off with: âEverything you thought you knew about binary data and Unicode has changed.â
2 Someone whoâs really amazingly together. We mean, who really knows where their towel is.
3 Hereâs a link to a high-level list of changes to Pythonâs Standard Library.
4 The reference implementation accurately reflects the languageâs definition. Its behavior is how all other implementations should behave.
5 C extension modules are written in C for use in Python.
Get The Hitchhiker's Guide to Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.