Scratchpad:Python 3

This page is about the steps to take to get out code to run with python 3.

Most of the external modules we use already support python 3. The 2to3 tool can fix some of the incompatibilities between python 2 and python 3. However, not all of them. The code base will be unstable afterwards.

Replacing code in trunk - DONE
First thing to do is to rework any code in trunk which will cause problems with python 3. This means we change trunk that the code works with python 2 and will also run fine with python 3.
 * 1) The cmp argument of the sort method. - done
 * 2) Replace sqlalchemy by alembic - done
 * 3) Migrate from beautifulsoup 3 to beautifulsoup 4 (How to port the code).- done
 * 4) No sqlite2 support for python 3. - done (code removed)
 * 5) There is no python 3 of bzrlib. Code has to be replaced (use command line, like on windows). - done

Prepare a branch with changes the 2to3 tool cannot fix
Running the 2to3 tool it not enough (obviously). There are a few changes which are not compatible with python 2, thus have to be prepared in a separate branch.
 * 1) Convert constructors to use supper.

I set up a branch for such changes: lp:~openlp-core/openlp/python3-productive

2to3 tool
Once we have done the two steps above we can run the 2to3 tool on the python3-productive branch.

How to run the tool:

2to3 --write --nobackups --no-diffs -x next print * && sh scripts/generate_resources.sh

If you have multi-cores you can add the -j NUMBER switch to make use of this. In terms of running openlp with python 3 we have to regenerate the resources.py files. Note: The bazaar commands shelve and unshelve might be useful here. ("Temporarily set aside some changes from the current tree.")

Note: We should run the 2to3 tool when everything else is done, due to conflicts. When the changes made by it are committed many (all?) merges from trunk will cause conflicts.

External modules
Some modules need special treatment or are not available for python 3 by the time of writing:
 * 1) pyinstaller does not have python 3 support yet

Miscellaneous

 * 1) LibreOffice does not work
 * 2) Powerpoint is not tested.
 * 3) You can you the -3 switch to show warnings about py3 incompatibilities which the 2to3 tool cannot fix.

Windows
IMPORTANT

Install 32bit versions of everything, even if you have a 64bit system. This is because there is currently no 64bit version of pyenchant.

Installation
Python 3.3. http://www.python.org/getit/

Windows extensions. http://sourceforge.net/projects/pywin32/files/

Install the python3 PyQt4 with Qt4.8. Not the one with Qt5, as it doesn't include phonon which is needed. http://www.riverbankcomputing.co.uk/software/pyqt/download

Install http://www.microsoft.com/en-us/download/details.aspx?id=35 (this was required for pyqt4 with qt5 so may be redundant with 4.8)

Instead of easy_install, use pip: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pip (I had sandbox errors occur with easy_install)

I had to get PIP from https://sites.google.com/site/pydatalog/python/pip-for-windows. After downloading run pip-Win_1.6.exe. Select c:\python33\python.exe as the Python Interperter. There will be several "Pauses" you must respond to in the Command window that is displayed.

c:\python33\scripts\pip install alembic mako beautifulsoup4 chardet pyodbc

If you get an error installing pyodbc you may get it from http://code.google.com/p/pyodbc/downloads/detail?name=pyodbc-3.0.7.win32-py3.3.exe

ICU from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyicu

lxml from http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml

pyenchant from https://pypi.python.org/pypi/pyenchant/1.6.5

After installing all packages, you can check your install by running ..\trunk\scripts\check_dependencies.py.

You should see the following: Python >= 3.0 ... 3.3.2.final.0 Checking for modules... PyQt4... OK PyQt4.QtCore... OK PyQt4.QtGui... OK PyQt4.QtNetwork... OK PyQt4.QtOpenGL... OK PyQt4.QtSvg... OK PyQt4.QtTest... OK PyQt4.QtWebKit... OK PyQt4.phonon... OK sqlalchemy... OK alembic... OK sqlite3... OK lxml... OK chardet... OK enchant... OK bs4... OK mako... OK uno... FAIL Checking for optional modules... MySQLdb (MySQL support)... FAIL psycopg2 (PostgreSQL support)... FAIL nose (testing framework)... FAIL mock (testing module)... FAIL Checking for Windows specific modules... win32com... OK win32ui... OK pywintypes... OK pyodbc... OK icu... OK Verifying version of modules... PyQt4 >= 4.6 ... 4.10.3 Qt4 >= 4.6 ... 4.8.5 sqlalchemy >= 0.5 ... 0.8.2 enchant >= 1.6 ... 1.6.5 Qt4 image formats...  read: b'bmp', b'pbm', b'pgm', b'png', b'ppm', b'xbm', b'xpm' write: b'bmp', b'png', b'ppm', b'xbm', b'xpm' Enchant (spell checker)...  available backends: ispell, myspell available languages: de_DE, en_AU, en_GB, en_US, fr_FR

Process finished with exit code 0

UNO is used by other platforms so it will show as "FAIL".

Running branch lp:~raoul-snyman/openlp/python3-2to3 This has had py2-3 run as well as generate_resources.sh

Problems
First time it ran it crashed after the UI came up, with the following on the console... http://openlp.codepad.org/sgkkXeJU It hasn't occurred since.

First time I tried libreoffice, python crashed after the presentation exited. Again it has worked subsequently.

Webkit video/background video doesn't work, but then this was hit-and-miss before. Phonon/VLC appear fine.