Main Content

Moving from Python 2.6 to Python 3

Archive - Originally posted on "The Horse's Mouth" - 2010-07-14 20:27:58 - Graham Ellis

When should you move your development to Python 3? When you're good and ready and you're certain that you'll not need to run your code on Python 2 again. Up to that point, the 2to3 utility will convert code for you.

Here's an example where I converted a short example script called "london" - using the -w option to write the file back, moving the old file to a .bak file. You can see in the reports the changes that have been made.

wizard:python graham$ 2to3 -w london
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
--- london (original)
+++ london (refactored)
@@ -2,7 +2,7 @@
 
 vatrates = (15,17.5,20) ;
 
-paid = float(raw_input("Whatdya pay furit? "))
+paid = float(input("Whatdya pay furit? "))
 
 for vatrate in vatrates:
  factor = 100.0 / (100.0 + vatrate)
@@ -10,6 +10,6 @@
  shopkeeper = paid * factor
  osborne = paid - shopkeeper
 
- print "Vat rate",vatrate,", Storeman ",shopkeeper,", Chancellor ",osborne
+ print("Vat rate",vatrate,", Storeman ",shopkeeper,", Chancellor ",osborne)
 
- print "izedone"
+ print("izedone")
RefactoringTool: Files that were modified:
RefactoringTool: london
wizard:python graham$


See [unconverted] and [converted] code.


Picture - delegate on a Python Course earlier this week. (You learn a lot on our courses - and we make them fun! as well.