GEPS 031: Python 3 support
GEPS Closed This GEPS (Gramps Enhancement Proposal) is closed and available in the version of Gramps indicated below. |
Python 3 support added for the release of Gramps 4.0
Python 2 support dropped for the release of Gramps 4.2 The Python.org team chose a sunset date for Python 2 of 1 Jan 2020.
Gramps was written with python 2. Slowly the default python is becoming python 3. Gramps 4.0 will require python 2.7+.
Ideally we also support python 3. Then in 2014-15 we can drop python 2.7 support. For convenience, we aim for python 3.2+
Contents
Guideline
No python 2.7 only constructs should be present, and no python 3.2 only constructs should be present.
Can I work with both pythons?
On the same family tree, normally not! For python3, bsddb is no longer a core package. You need to install python3-bsddb, and that will normally be a more recent version than what is present in python 2.7.
So, once you open a family tree with python3, you will not be allowed to open it with python 2.7. The only way to open it with python 2.7 is using the bsddb3 option of Gramps, and installing from source the bsddb3 package for python 2.7 that is as current as the one given in python3
Tips
- If you work with python3, edit the ~/.subversion/config files, and add the section
global-ignores = __pycache__
- Give a family tree a name that contains 32 if you use python 3, and 27 if you use python 2. Once you open a python 2 family tree, the database is upgrade and you canot open it again in python 2 (you could export to xml and import in python 2 though)
How?
See patchset in [1].
Main attention points:
- we should not use unicode anymore. Instead, import from gramps.gen.constfunc the cuni or friends functions to convert to unicode
- we should not compare to basestring or unicode type. Instead use UNITYPE and STRTYPE from gramps.gen.constfunc
- in python 3 many functions became iterators. Remove the old 2.7 names and replace with the 3.2 names as much as possible. Only when performance is really an issue, use an if construct on python version so that python 2.7 also uses an iterator.
- you will see a lot of list(map ...) or list(range...) after conversion. Only remove this if you studied the code and know it will work in python 2 and 3
- If special unicode symbols are needed, then use from __future__ import unicode_literals and # -*- coding: utf-8 -*- and adapt the 2.7 code to have it working with this.
- to test on python version, use if sys.version_info[0] < 3: for consistency with the other patches.
Requirements
Ubuntu 12.10
Python 3.2.3 ships with Ubuntu 12.10. To run Gramps, you will also need the packages:
- python3-gi: gobject introspection
- python3-gi-cairo: cairo gobject instrospection
- python3-cairo : This is needed to have import cairo work
- python3-bsddb3: This is needed to have import bsddb3 work
Related Gramps Bugs
- Enhancement Request #2620: GEPS 031: Python 3 support - 3.2
Lost - 1 of 4495 issues disappeared due to a table lost during a MantisBT upgrade. Per a developer maillist posting, this Python 3.0 compatibility enhancement was completed in 2009 for the Gramps 3.2 version.
See also: GEPS 032 - Python 3 Deprecated