Python logo

Python 2 vs. Python 3

Changes, support and porting

Gerald Senarclens de Grancy, gerald@senarclens.eu

Which version
should I use?

Which Version Should I Use?

Short version:

Python 2.x is the status quo.

Python 3.x is the present and future of the language.

Libraries

Not available
  • gevent
  • ...
Currently ported

Python 3 porting status

Available libraries

Current Linux distributions offer many Python 3 libraries

600+ libraries/modules already ported to Python 3 on PyPI

  • cython
  • GUI: Tkinter, PyQt4, PySide, PyGObject
  • matplotlib >= 1.2
  • nose
  • numpy
  • pip
  • Web: django >= 1.5, Pyramid >= 1.3, CherryPy
  • ...

sudo pip-${PYTHON-VERSION} install ${PACKAGE}

Use Python 3
whenever possible.

Differences by Example

Use a good reference! (available as Android app ;)

Case Study - Porting an Existing Program

  1. Prerequisites
    • Make sure you have a comprehensive battery of tests
    • Ensure that required libraries are available for Python 3
    • pip3 and nosetests3 are your friends
  2. Run the tests to see if they pass
  3. Commit the current state to a VCS
  4. Run 2to3
    1. Convert multiple files by passing a directory
    2. 2to3 -w dirname/
    3. Later, consider 2to3 -w -f idioms dirname/
    4. Shebangs are not adjusted
  5. Manual porting "loop"
    1. Run the tests
    2. Fix failures
    3. Stop if all tests pass
  6. Run a view manual system tests
    • If errors occur, create new unit tests
    • Fix remaining issues
  7. Commit the result to a VCS (maybe a new branch)

Conclusion

Further Reading

Mark Pilgrim Dive Into Python 3 (2nd edition) Apress (October 23, 2009)
Python Software Foundation Python Documentation http://docs.python.org/
Python Wiki Porting Python Code to 3.0 http://wiki.python.org/moin/PortingPythonToPy3k
Python Wiki Should I use Python 2 or Python 3 for my development activity? http://wiki.python.org/moin/Python2orPython3
Lennart Regebro Porting to Python 3: An in-depth guide Createspace (2. März 2011)
Guido van Rossum PEP 3000 -- Python 3000 http://www.python.org/dev/peps/pep-3000/

Questions
and feedback...