Picking a Python Interpreter (3 vs 2)
The State of Python (3 & 2)
When choosing a Python interpreter, one looming question is always present:“Should I choose Python 2 or Python 3”? The answer is a bit more subtle thanone might think.
The basic gist of the state of things is as follows:
- Most production applications today use Python 2.7.
- Python 3 is ready for the production deployment of applications today.
- Python 2.7 will only receive necessary security updates until 2020 [6].
- The brand name “Python” encapsulates both Python 3 and Python 2.
Recommendations
Note
The use of Python 3 is highly preferred over Python 2. Consider upgrading your applications and infrastructure if you find yourself still using Python 2 in production today. If you are using Python 3, congratulations — you are indeed a person of excellent taste.—Kenneth Reitz
I’ll be blunt:
- Use Python 3 for new Python applications.
- If you’re learning Python for the first time, familiarizing yourself with Python 2.7 will be veryuseful, but not more useful than learning Python 3.
- Learn both. They are both “Python”.
- Software that is already built often depends on Python 2.7.
- If you are writing a new open source Python library, it’s best to write it for both Python 2 and 3simultaneously. Only supporting Python 3 for a new library you want to be widely adopted is apolitical statement and will alienate many of your users. This is not a problem — slowly, over the next three years, this will become less the case.
So…. 3?
If you’re choosing a Python interpreter to use, Irecommend you use the newest Python 3.x, since every version brings new andimproved standard library modules, security and bug fixes.
Given such, only use Python 2 if you have a strong reason to, such as apre-existing code-base, a Python 2 exclusive library, simplicity/familiarity,or, of course, you absolutely love and are inspired by Python 2. No harm in that.
Check out Can I Use Python 3? to see if anysoftware you’re depending on will block your adoption of Python 3.
It is possible to write code that works on Python 2.6, 2.7, and Python 3. Thisranges from trivial to hard depending upon the kind of softwareyou are writing; if you’re a beginner there are far more important things toworry about. Note that Python 2.6 is end-of-life upstream, so you shouldn’ttry to write 2.6-compatible code unless you’re being paid specifically todo that.
Implementations
When people speak of Python they often mean not just the language but alsothe CPython implementation. Python is actually a specification for a languagethat can be implemented in many different ways.
CPython
CPython is the reference implementation of Python,written in C. It compiles Python code to intermediate bytecode which is theninterpreted by a virtual machine. CPython provides the highestlevel of compatibility with Python packages and C extension modules.
If you are writing open source Python code and want to reach the widest possibleaudience, targeting CPython is best. To use packages which rely on C extensionsto function, CPython is your only implementation option.
All versions of the Python language are implemented in C because CPython is thereference implementation.
PyPy
PyPy is a Python interpreter implemented in a restrictedstatically-typed subset of the Python language called RPython. The interpreterfeatures a just-in-time compiler and supports multiple back-ends (C, CLI, JVM).
PyPy aims for maximum compatibility with the reference CPython implementationwhile improving performance.
If you are looking to increase performance of your Python code, it’sworth giving PyPy a try. On a suite of benchmarks, it’s currently over 5 timesfaster than CPython.
PyPy supports Python 2.7. PyPy3 [1], released in beta, targets Python 3.
Jython
Jython is a Python implementation that compilesPython code to Java bytecode which is then executed by the JVM (Java Virtual Machine).Additionally, it is able to import and use any Java class like a Pythonmodule.
If you need to interface with an existing Java codebase or have other reasons toneed to write Python code for the JVM, Jython is the best choice.
Jython currently supports up to Python 2.7. [2]
IronPython
IronPython is an implementation of Python for the .NETframework. It can use both Python and .NET framework libraries, and can alsoexpose Python code to other languages in the .NET framework.
Python Tools for Visual Studio integratesIronPython directly into the Visual Studio development environment, making itan ideal choice for Windows developers.
IronPython supports Python 2.7. [3]
PythonNet
Python for .NET is a package whichprovides near seamless integration of a natively installed Pythoninstallation with the .NET Common Language Runtime (CLR). This is theinverse approach to that taken by IronPython (see above), to which itis more complementary than competing with.
In conjunction with Mono, pythonnet enables native Pythoninstallations on non-Windows operating systems, such as OS X andLinux, to operate within the .NET framework. It can be run inaddition to IronPython without conflict.
Pythonnet supports from Python 2.6 up to Python 3.5. [4][5]
[1] | http://pypy.org/compat.html |
[2] | https://hg.python.org/jython/file/412a8f9445f7/NEWS |
[3] | http://ironpython.codeplex.com/releases/view/81726 |
[4] | https://travis-ci.org/pythonnet/pythonnet |
[5] | https://ci.appveyor.com/project/TonyRoberts/pythonnet-480xs |
[6] | https://www.python.org/dev/peps/pep-0373/#id2 |