Python 3 Guide

Joachim Metz edited this page Jul 3, 2017 · 39 revisions

Python 3 Guide

plaso is Python 3 compatible, but not all of its dependencies are.

This page contains information about which Python language features to use to help plaso to stay Python 2.7 compatible and stay Python 3.4 and later compatible.



dict.iteritems() => iter(dict.items())


  • The result of \ is a floating point, use divmod() instead (or \\)
  • long() and 1L no longer work


  • % format notation on longer supported, replaced by format and {} notation
  • explicitly mark byte strings (b'')
  • str is Unicode not bytes so str.decode fails
  • Use __unicode__ in preference of __str__
  • unicode() is no longer supported
  • basestring is no longer supported

Make the default string type Unicode.

from __future__ import unicode_literals


In Python 3 print is a function:

print "Test" => print("Test")

For compatibility with Python 2, and to stop pylint complaining, add the following import:

from __future__ import print_function


StringIO.StringIO is replaced by io.StringIO and io.BytesIO



The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error.
if sys.version_info[0] < 3:
  import urllib2 as urllib_error
  from urllib2 import urlopen
  import urllib.error as urllib_error
  from urllib.request import urlopen


xrange() is no longer supported by Python 3 use range() instead:

xrange(10) => range(0, 10)


TypeError: 'map' object is not subscriptable


map(int, [1])[0]

In Python 3 map() returns a map where in Python 2 this was a list e.g.

type(map(int, [1]))

A solution is to wrap map in a list.

Other similar errors are:

TypeError: unorderable types: map() < map()


In Python 3 filter() returns a filter where in Python 2 this was a list e.g.

type(filter(None, []))

A solution is to wrap filter in a list.

To do

from __future__ import absolute_import
from __future__ import division

Octal integers are written in a different form e.g. instead of 0666 now 0o666

C extensions


Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.