Python Forum
How to use 2to3.py converter under Windows OS
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use 2to3.py converter under Windows OS
#1
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
Reply
#2
(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)
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  currency converter using forex-python preethy12ka4 8 677 Mar-08-2024, 06:59 PM
Last Post: preethy12ka4
  Help with basic weight converter PythonSquizzel 3 1,399 Jun-29-2022, 02:42 PM
Last Post: deanhystad
  Converter Souls99 2 2,388 Aug-01-2020, 01:27 AM
Last Post: Souls99
  Help me get this image converter code working? NeTgHoSt 0 2,081 Jul-14-2020, 10:36 PM
Last Post: NeTgHoSt
  Currency converter Scott 5 2,989 Jun-14-2020, 11:59 PM
Last Post: Scott
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,825 Dec-30-2018, 04:07 PM
Last Post: Gribouillis
  Error during 2to3 conversion krow4869 3 3,972 Oct-14-2018, 08:19 AM
Last Post: Larz60+
  Trying to convert a script from "2to3" JohnathanMonkey 15 10,131 Jul-31-2017, 08:05 PM
Last Post: Majora
  Can't get a downloaded file converter to work Godotisnothere 1 3,170 Jan-24-2017, 06:01 PM
Last Post: Larz60+
  2to3 fails on very simple script Skaperen 21 17,231 Oct-20-2016, 08:23 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020