We’ve recently been migrating our code from Python 2 to Python 3. There is a lot of documentation about the changes, but these are changes we had to make in our code.
First, the print statement had to be changed to the print function:
print 'message'
became
print('message')
Text and bytes
Python 3 change bytes and unicode text handling, so here some changes related to that:
json.dumps required a unicode string, instead of bytes, so
json.dumps(xml.serialize())
became
json.dumps(xml.serialize().decode('utf8'))
basestring was removed, so
isinstance("", basestring)
became
isinstance("", str)
This change to explicit unicode and bytes handling affected the way we opened files. In Python 2, we could open and use a binary file, without specifying that it was binary:
open('file.zip')
In Python 3, we have to specify that it’s a binary file:
open('file.zip', 'rb')
Some functions couldn’t handle unicode in Python 2, so in Python 3 we don’t have to encode the unicode as bytes:
urllib.quote(u'tëst'.encode('utf8'))
became
urllib.quote('tëst')
Of course, Python 3 reorganized parts of the standard library, so the last line would actually be:
urllib.parse.quote('tëst')
Dicts
There were also some changes to Python dicts. The keys() method now returns a view object, so
dict.keys()
became
list(dict.keys())
dict.iteritems()
also became
dict.items()
Virtual environments
Python 3 has virtual environments built in, which means we don’t need to install virtualenv anymore. There’s no activate_this.py in Python 3 environments, though, so we switched to using django-dotenv instead.
Miscellaneous
Some more changes we made include imports:
from base import * => from .base import *
function names:
func.func_name => func.__name__
and exceptions:
exception.message => str(exception) except Exception, e => except Exception as e
Optional
Finally, there were optional changes we made. Python 3 uses UTF-8 encoding for source files by default, so we could remove the encoding line from the top of files. Also, the unicode u” prefix is allowed in Python 3, but not necessary.