Umstellung von Python 2 auf Python 3

Ab rmDATA GeoMapper Version 2023.3 wird für das Skripting Python 3 eingesetzt. Daher ist es notwendig, alle Skripts von Python 2 auf Python 3 zu konvertieren.
Im Folgenden sind die notwendigen Änderungen beschrieben. Alternativ können Sie auch ein Tool nutzen und alle Skripts automatisch konvertieren lassen.

Notwendige Änderungen für IronPython 3

  • Aufruf von print:
✎

Python 2

print "text"

Python 3

print ("text") 
  • Formatierung und Ausgabe von Gleitkommazahlen: die Anzahl der Nachkommastellen muss jetzt immer angegeben werden.
✎

Python 2

print str(3.1234567) erzeugt in Python 2 3.1234567

Python 3

print (str(3.1234567)) erzeugt in Python 3 3.1234567000000002

Korrekte Angabe in Python 3, z.B. auf 4 Nachkommastellen:

print('{:.4f}'.format(3.123456)) erzeugt in Python 3 3.1235

  • Mit 0 beginnende Dezimalwerte sind nicht mehr erlaubt. Die Angabe von month = 06 führt zu einer Fehlermeldung.
✎

Korrekte Angabe: month = 6

  • Die Bibliothek vectors.py ist nicht mehr Teil des Standard-Umfangs. GeoMapper installiert diese Bibliothek trotzdem mit. Die Klasse „Vector“ kann daher weiterhin verwendet werden.

Migration von Skripten von IronPython 2.7.5 auf 3.4.10

Für die massenhafte Migration von py-Dateien steht ein Tool zur Verfügung. So gehen Sie vor:

  • Download und Installation Python 3.4: https://www.python.org/downloads/release/python-340/
  • Danach steht python.exe in der Kommandozeile zur Verfügung
  • Aufruf Kommandozeile: python.exe 2to3.py „Skriptpfad.py" –w
  • Die Datei wird migriert, die alte Datei wird als Skriptpfad.py.bak gesichert
  • Es funktioniert auch als Batch-Datei (bat):

python.exe 2to3.py "C:\Samples\TextNew.py" -w
python.exe 2to3.py "C:\Samples\Union.py" -w
python.exe 2to3.py "C:\Samples\werteEingabe.py" -w
pause

ⓘ

Speichern Sie die bat-Datei im Format „Westeuropäischer Zeichensatz (Codepage 850)“ ab. Dann funktioniert auch die Migration von Skripten mit Umlauten im Pfad.

⚠

Eine manuelle Nachbearbeitung inkl. einer Funktionspruefung des Skripts ist notwendig!

ⓘ

Die automatische Migration ändert month = 06 in month = 0o6. Der Präfix 0o muss entfernt werden, da es sich sonst um eine Oktalzahl handelt.