Add Python 3 support to Python 2 applications using the six module.
sixer is a tool adding Python 3 support to a Python 2 project.
sixer was written to produces patches to port OpenStack to Python 3.
It uses basic regular expressions to find code which needs to be modified. It emits warnings when code was not patched or looks suspicious.
sixer.py <directory or filename> <operation>
sixer.py displays the name of patched files. It displays warnings for code unchanged but which looks suspicious.
If you pass a directory, sixer.py searchs for .py files in all subdirectories.
Warning: sixer.py modifies files inplace without asking for confirmation. It’s better to use it in a project managed by a source control manager (ex: git).
To install sixer, type:
pip3 install sixer
sixer requires Python 3, it doesn’t work on Python 2.
combine all operations all together
replace basestring with six.string_types
replace dict.iteritems() with six.iteritems(dict)
replace dict.itervalues() with six.itervalues(dict)
replace dict.iterkeys() with six.iterkeys(dict)
note: for key in dict.iterkeys(): can usually be simplified to for key in dict:
replace 123L with 123
octal number are unchanged (ex: 010L)
replace iter.next() with next(iter)
replace raise exc, exc, exc with six.reraise(*exc)
replace raise exc_type, exc_value, exc_tb with six.reraise(exc_type, exc_value, exc_tb)
replace raise exc, msg with raise exc(msg)
replace Python 2 imports with imports from six.moves
Python 2 modules:
replace Python 2 urllib and urllib2 with six.moves.urllib
replace StringIO.StringIO with six.StringIO
replace cStringIO.StringIO with moves.cStringIO
replace from StringIO import StringIO with from six import StringIO
replace from cStringIO import StringIO with from six.moves import cStringIO as StringIO
later you may have to replace it with six.BytesIO (or io.BytesIO if you don’t support Python 2.6) when bytes are expected on Python 3
replace unicode with six.text_type
replace xrange() with range() and add from six.moves import range
don’t add the import if all ranges have 1024 items or less
Adding the six import
When an operation uses six, import six may be added. sixer repects OpenStack coding style rules to add the import: imports grouped by standard library, third party and local imports; and imports must be are sorted.
The sixer tool was initially written to produce patches for OpenStack which respects OpenStack coding style, especially the complex rule to group and sort imports.
The project is based on regular expressions, it produces false positives (invalid changes). For example, some operations replace patterns in strings, comments or function names even if it doesn’t make sense.
Try also the 2to6 project which may be more reliable.
To run tests, type tox. Type pip install tox to install the tox program.
Or run tests manually: type python3 tests.py.
Version 0.5 (2015-07-08)
six_moves: support “import module as name” syntax and add cPickle module
Add –to-stdout, –quiet and –max-range command line options
Emit a warning if the directory does not contain any .py file or if the path does not exist
Test also directly the sixer.py program
Version 0.4 (2015-06-09)
sixer.py now accepts multiple filenames on the command line, but operations becomes the first command line parameter
the stringio operation now also replaces cStringIO and from StringIO import StringIO
urllib: replace also urlparse.symbol
six_moves: support more modules: Cookie, HTMLParser, SimpleHTTPServer, cookielib, xmlrpclib, etc.
Refactor operations as classes to cleanup the code
Version 0.3.1 (2015-05-27)
Fix the “all” operation
six_moves knows more modules
urllib: add pathname2url, don’t touch urllib2.parse_http_list()
Version 0.3 (2015-05-27)
First command line parameter can now be a filename
Add “all”, “basestring”, “iterkeys”, “six_moves”, “stringio” and “urllib” operations
Enhance the knownledge tables for modules (stdlib, third parties, applications)
Ignore unparsable import lines when adding an import
Version 0.2 (2015-05-12):
First public release
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.