Notes on migrating packages from Python 2.7 to Python 2/3 cross-compatibility
General resources
- https://docs.python.org/3/howto/pyporting.html (more conceptual)
- http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html (more practical)
six
is a very helpful library for cross-version imports of things likeurllib
- there are some automatic conversion utilities like futurize, but i'm skeptical
Dependencies
- check whether dependencies are already cross-compatible
- if newer dependency versions support only Python 3, you may need to do extra testing and/or adjust how the dependencies are specified
- e.g., Matplotlib 3.x supports only Python 3, and pip at one point was having trouble figuring out what to install for Python 2.7
Updating syntax
- print statements:
print
→print()
(latter is cross-version) - integer division: can be implicit in Python 2.7, but must be explicit in Python 3
- unicode handled natively by the
str
type in python 3 (check places where you read or write data) - exceptions:
raise Error, "error"
→raise Error("error")
(latter is cross-version) urllib
: info 1, info 2