Python Forum

Full Version: How to use 2to3.py converter under Windows OS
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
During my recent revisiting of Python world, I realized that the pretty feature (2to3) for files conversion from Python2 to Python3 is suitable only for unixlike systems as shown in this url: https://docs.python.org/3/library/2to3.html

What happens if I'm confined to Windows operating system?

My trick is as follows. First of all, put inside any working folder this file (2to3.py)
#!/usr/bin/env python
from lib2to3.main import main
import sys
import os
sys.exit(main("lib2to3.fixes"))
Then, copy inside the file to be converted (example.py):
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
Now, from console give the command:
python 2to3.py example.py
In few seconds the new code suitable for Python3 code is formed with the same name (example.py), while the original Python2 file is automatically saved with appropriate suffix example.py.bak
# ------- example.py for Python3 -------
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)

# ------ example.py.bak, original under Python2 -----
def greet(name):
    print "Hello, {0}!".format(name)
print "What's your name?"
name = raw_input()
greet(name)
Finally, the option -w can be used for showing, step by step, all changes
python 2to3.py -w example.py
All the best
(Mar-01-2019, 08:52 PM)samsonite Wrote: [ -> ]I realized that the pretty feature (2to3) for files conversion from Python2 to Python3 is suitable only for unixlike systems as shown in this url: https://docs.python.org/3/library/2to3.html
That not right it work same under Windows and Linux.
I have used 2to3 many times before on Windows.
A quick run,i usually use it folder where 2to3.py is placed python37\Tools\scripts
C:\python37\Tools\scripts
λ python 2to3.py -w greet.py
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored greet.py
--- greet.py    (original)
+++ greet.py    (refactored)
@@ -1,5 +1,5 @@
 def greet(name):
-    print "Hello, {0}!".format(name)
-print "What's your name?"
-name = raw_input()
+    print("Hello, {0}!".format(name))
+print("What's your name?")
+name = input()
 greet(name)
RefactoringTool: Files that were modified:
RefactoringTool: greet.py

# Look at result after run
C:\python37\Tools\scripts
λ cat greet.py
def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)
Good drift, snippsat.

Despite some guides suggestions, my Python release 3.7.2 doesn't use subfolder python37\Tools\Scripts, but the shorter python37\Scripts, so I've solved adequately, as per attachment: http://imgbox.com/qnoClsN7

Thanks a lot