http://docs.python.org/3/howto/pyporting.html
http://docs.pythonsprints.com/python3_porting/py-porting.html
1. Exception capturing old:
try:
raise Exception()
except Exception, exc:
# Current exception is 'exc'.
pass
Exception capturing new:
try:
raise Exception()
except Exception as exc:
# Current exception is 'exc'.
# In Python 3, 'exc' is restricted to the block; in Python 2.6/2.7 it will "leak".
pass
Compatible way:
try:
raise Exception()
except Exception:
import sys
exc = sys.exc_info()[1]
# Current exception is 'exc'.
pass
2. Compatibly opening text files containing unicode strings: codecs.open()
3. Text literals should have the u prefix
4. Umbenennungen
ConfigParster -> configparser
Tkinter -> tkinter
tkFileDialog -> tkinter.filedialog
tkFont -> tkinter.font
tkMessageBox -> tkinter.messagebox
try:
import OLDNAME
except ImportError:
import NEWNAME
5. print Inkompatibel
Python 2:
print 5,6 -> 5 6
print(5,6) -> (5,6)
Python 3:
print 5,6 -> ERROR
print(5,6) -> 5 6
print((5,6)) -> (5,6)
6. sets deprecated since 2.6
(set and frozenset are builtin types since 2.4)
Solution: rename Set to set, ImmutableSet to frozenset, and do at the beginning:
try:
set # succeeds from 2.4
except NameError: # 2.3 or earlier
from sets import Set as set, ImmutableSet as frozenset