Rewrite Python2 code to Python3 (2to3)

Nariyuki Hmm? ?? This is Python2 code! ?? I'm not sure about Python3, but forgive me ...

Python fucking beginners are writing like that.

Environment Windows10 Python 3.7.4

This time, everything is done at the command prompt.

Because it is assumed that Python3 is already installed Try typing py </ code> at the command prompt to see if there are any errors. ( quit () </ code> exits Python's interactive mode)

2to3

Apparently, it ’s a Python library program called 2to3. It seems that it will automatically convert the Python2 code to the Python3 code.

Practice (mistake) Suppose you want to convert "Hello.py" placed on your desktop.

First, move to the location where the file you want to convert is (in this case, the desktop). cd C: \ Users \ (user name) \ Desktop </ code> Next, finally convert from Python2 to Python3! !! 2to3 -w Hello.py

Now "Hello.py" is the code for Pyhon3 The original Python2 code should be "Hello.py.bak" !! ??

 '2to3' is an internal or external command,
 It is not recognized as an operable program or batch file.

Error ...

Apparently This seems to be OK on other than Windows, but an error occurs on Windows.

Practice (success) Suppose you want to convert "Hello.py" that you put on your desktop as above. If Python is installed in C: \ Python37
 cd C: \ Users \ (user name) \ Desktop
python C:\Python\Python37\Tools\scripts\2to3.py -w hello.py

I got it! !!

The parts you need to change to use

  • Path to the location of the file you want to convert C: \ Users \ (username) \ Desktop
  • Path to 2to3.py C: \ Python \ Python37 \ Tools \ scripts \ 2to3.py
  • File name of the file you want to convert hello.py
What a place, such as

Reference site Use 2to3 on Windows (Conversion of 2 series code to 3 series code)-Memo warehouse https://woraise.com/2019/03/11/2to3/

Conversion tool from 2 system to 3 system-Python-izm https://www.python-izm.com/tips/2to3/

2to3 --Automatic code conversion from Python 2 to 3 -Python https://docs.python.org/ja/3/library/2to3.html

Recommended Posts