Compatibility with Python versions.¶
Theano is compatible with Python versions >= 2.6 including 3.x series. This guide provides coding guidelines on how to maintain comnpatibility.
Installation (2to3 and setup.py)¶
Python code compatibility¶
Ideally, Python 3-incompatible changes should be caught by the Travis build, but here are some guidelines for the major things to be aware of.
When catching an exception and giving a name to it, use the new-style syntax:
except Exception as e: ...
No tuple-unpacking in the argument list:
def f(a, (b, c)): # wrong ... def f(a, others): b, c = others # right
Always use
print(...)
as a function, and addfrom __future__ import print_function
At the top of files that use
print
, above all other imports.If you mean to iterate over a range, use
six.moves.xrange
. If you need a range as a list, uselist(range(...))
.If you mean to iterate over a zipped sequence, use
theano.compat.izip
. If you actually need it to be a list, uselist(zip(...))
.If you mean to iterate over the output of a
map
, usetheano.compat.imap
. If you actually need it to be a list, uselist(map(...))
.If using
reduce
,StringIO
,pickle
/cPickle
, orcopy_reg
, use the one insix.moves
(note thatcopy_reg
is namedcopyreg
).For iterating over the items, keys, values of a list, use
six.iteritems(...)
,six.iterkeys(...)
,six.itervalues(...)
. If you wish to grab these as lists, wrap them in alist(...)
call.Avoid
itertools.{izip,ifilter,imap}
. Use the equivalently named functions fromtheano.compat
.Don’t use __builtin__. Rather,
six.moves.builtins
.Rather than the three-argument version of
raise
, usesix.reraise(...)
.Don’t call the
.next()
method of iterators. Use the builtinnext(...)
.When type testing for
str
orint
/long
, usesix.string_types
andsix.integer_types
, respectively.Use the
six.add_metaclass
class decorator for adding metaclasses.
Compatibility package (theano.compat)¶
Compatibility between 2.x and 3.x is implemented using six, a Python
2 and 3 compatibility library by Benjamin Peterson. Additionally,
certain additional or modified functions are available in the theano.compat
module.
Strings, bytes and unicode¶
str
and bytes
are different types in Python 3. Mostly this doesn’t
come up for Theano’s purposes. str
represents encoded text, i.e. character
encoding-aware; all Python 3 strings are internally stored as Unicode. When
converting from one or the other, you must .encode(...) bytes and
.decode(...) strings with a given character encoding (e.g. 'ascii'
,
'utf8'
, 'latin1'
). Fortunately, these methods exist as no-ops
in Python 2.6 and 2.7.