Archive
2014: moving towards Python 3
My new year’s resolution is to move (slowly) towards Python 3. I don’t want to switch yet but I will use things in my code that will facilitate the transition in the future. Thus, from now on I will use this skeleton for my new scripts:
#!/usr/bin/env python
# encoding: utf-8
from __future__ import (absolute_import, division,
print_function, unicode_literals)
def main():
pass
####################
if __name__ == "__main__":
main()
“A future statement must appear near the top of the module. The only lines that can appear before a future statement are:
- the module docstring (if any),
- comments,
- blank lines, and
- other future statements.“
More info on the __future__ imports
Compatibility layers
Python 3 is slower than Python 2?
Recently I was playing with concurrent.futures. Following a comment on reddit, I got to the presentation of David Beazley entitled Understanding the Python GIL.
It’s a very interesting talk and from this I learned that Python 3.2 got a new GIL implementation! Out of curiosity I compared the performance of Python 2.7 and 3.3. The test machine had 4 cores. I made a CPU bound test script with three variations: (1) basic, single-threaded version, (2) using 4 threads, and (3) using 4 processes.
The results were surprising for me because Python 2.7 turned out to be faster!
(Legends: Py2 = Python 2.7.4, Py3 = Python 3.3.1)
basic.py:
Py2: 5.32 sec, Py3: 9.66 sec
with_threads:
Py2: 13.41 sec, Py3: 17.32 sec
with_processes:
Py2: 1.28 sec, Py3: 2.27 sec
You can also try the scripts, they are here.
What’s New In Python 3.0
What’s New In Python 3.0 by Guido @docs.python.org
Python 3 Wall of Shame
At Python 3 Wall of Shame you can monitor the compatibility status of several packages with Python 3.
Install Python 3.2 and/or Python 2.5 on Ubuntu 11.04
Update (20121120): Google App Engine supports Python 2.7 so there is no need any more to install Python 2.5. I consider this post outdated.
Chad Lung has a nice post on installing Python 3.2 from source on Ubuntu 11.04. The future versions of Ubuntu will come with Python 3, so maybe it’s a good time to start to discover Python 3 a bit.
Notes
The package “python3.2” exists in the Ubuntu repositories but it’s not up-to-date. If you want the latest version of Python, you’ll have to install it from source.
If you install Python 3.2 from source, make sure to install it with “./configure; make; sudo make altinstall“, where the emphasis is on altinstall. This way Python 3.2 will be installed next to your existing 2.x version(s), so your system won’t be messed up.
To figure out your exact Python version, you can do this:
>>> import sys >>> print(sys.version_info[:]) (3, 2, 0, 'final', 0)
Install Python 2.5 (update, 20110926)
Edit (20111112): Google App Engine now supports Python 2.7! At the moment it’s experimental but it works. So it’s very likely you don’t need Python 2.5 at all.
I wanted to try Google App Engline but it runs on Python 2.5 on the production servers at Google. So it’s better to test your applications locally with Python 2.5 too, otherwise there is a good chance that you develop something that runs fine on your machine but breaks at Google.
So, how to install Python 2.5 keeping the newer Python versions too? I tried the method that I described above with version 3.2 but “make” failed. But in this post I found a PPA from where you can install Python 2.5 easily. Steps to follow:
sudo add-apt-repository ppa:fkrull/deadsnakes sudo apt-get update sudo apt-get install python2.5
Now if you want to use Python 2.5, just modify the first line of your scripts:
#!/usr/bin/env python2.5
Update: I figured out how to compile 2.5 from source. “make” produced the following error message: “/usr/include/sqlite3.h: version 3.7.4”. After “configure”, open Makefile and edit this line:
# before: # LDFLAGS= # after: LDFLAGS= -L/usr/lib/i386-linux-gnu
After this compilation was done successfully with “make”. This tip is from here.
Should I use Python 2 or Python 3?
“Should I use Python 2 or Python 3?”
This is a very common question when someone wants to learn Python. Here is a nice article about this topic: http://wiki.python.org/moin/Python2orPython3.
(Thanks Jaume for the link.)
Update (20110404)
If you are ready to dive in Python 3, here are some tutorials:
- The official Python 3 tutorial (HTML, PDF)
- Further Python 3 docs (c-api.pdf, distutils.pdf, documenting.pdf, extending.pdf, faq.pdf, howto-advocacy.pdf, howto-cporting.pdf, howto-curses.pdf, howto-descriptor.pdf, howto-doanddont.pdf, howto-functional.pdf, howto-logging-cookbook.pdf, howto-logging.pdf, howto-pyporting.pdf, howto-regex.pdf, howto-sockets.pdf, howto-sorting.pdf, howto-unicode.pdf, howto-urllib2.pdf, howto-webservers.pdf, install.pdf, library.pdf, reference.pdf, tutorial.pdf, using.pdf, whatsnew.pdf)
- Dive Into Python 3 (HTML and PDF)
Update (20110526)
I follow the following simple guideline: I use that version of Python that comes with Ubuntu by default. In Ubuntu 10.10 it was Python 2.6, in Ubuntu 11.04 it’s Python 2.7. When they switch to Python 3.x, I will switch too.
