For the moment, we want a transition step where the code is both Python 2 and 3 compatible.
The general idea is to "patch" the code so as to have the least amount of work when switching to Python 3 only. For instance, when using
from six.moves import PY3name
from six.moves import foo as PY3name
from six.moves import foo as PY2name
Example of recurrent issues (non-exhaustive list, also serves as a guideline):
from six.moves.urllib import parse import six; six.iterkeys(...); from six.moves import configparser from six.moves.urllib.request import urlopen from six.moves import ( http_client as httplib, xmlrpc_client as xmlrpclib, ) from zope.interface import implementer @implementer(...) subprocess.Popen(..., universal_newlines=True, ...)
Strings and encoding
Everything you thought you knew about binary data and Unicode has changed.
- Python 3.0 uses the concepts of text and (binary) data instead of Unicode strings and 8-bit strings. [...]
- As the str and bytes types cannot be mixed, you must always explicitly convert between them. Use str.encode() to go from str to bytes, and bytes.decode() to go from bytes to str. You can also use bytes(s, encoding=...) and str(b, encoding=...), respectively.
|Python 2||Python 3|
I recommend using the