63

I have some code in python 2.7 and I want to convert it all into python 3.3 code. I know 2to3 can be used but I am not sure exactly how to use it.

nbro
  • 12,226
  • 19
  • 85
  • 163
GhostFrag1
  • 845
  • 2
  • 11
  • 19

9 Answers9

68

Install the following module which adds the 2to3 command directly to entry_points.

pip install 2to3

As it is written on 2to3 docs, to translate an entire project from one directory tree to another, use:

2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode
Smart Manoj
  • 3,837
  • 2
  • 24
  • 45
Faruk Sahin
  • 7,418
  • 5
  • 25
  • 31
  • 1
    Can I use the 2to3 file that is in the scripts folder of python 2.7? Because I hear that cmd is needed. – GhostFrag1 Dec 08 '13 at 19:43
  • Yes, you can use that one. You have to open up a console(cmd if windows) and execute that file supplying the right arguments as written in the answer. – Faruk Sahin Dec 08 '13 at 19:49
  • 2
    Still not too sure how to use exactly. Could you provide a few extra details please? – GhostFrag1 Dec 08 '13 at 20:22
  • I assume you are on windows environment since you mentioned about cmd. Basically 2to3 is a python script that you need to run through console. If you are having trouble finding 2to3 on path, try "python path/to/script/2to3.py arguments". This expects python to be in the path though. If you are facing further problems try to google "how to execute a python script in windows" or more specifically "how to execute 2to3 in windows" – Faruk Sahin Dec 08 '13 at 20:32
  • 1
    I seem to be getting an error saying RefactoringTool: Can't open No such file or directory – GhostFrag1 Dec 08 '13 at 21:05
  • @FarukSahin When I did this on Ubuntu, I ended up with a folder named `=` that contained the converted py3 files so I had to rename the folder to `py3VersionMyCode`. – mLstudent33 Sep 07 '19 at 10:26
  • Does this apply all the "fixers"? When I ran it initially, for example, it did not convert my except statements and I had to do that with -f except. – demongolem Apr 09 '20 at 14:45
34

If you don't have 2to3 on your path, you can directly invoke lib2to3:

python -m lib2to3 directory\file.py

And as the docs (and other answers) mention, you can use some flags for more customization:

  • the -w flag to enable writeback, which applies the changes to the file
  • the -n to disable backups

(there are a few more flags; see the docs for more information.)

Graham
  • 1,855
  • 1
  • 13
  • 28
16

It's important to have a backup before running 2to3.

  1. If you're using git, make a commit.
  2. Otherwise, make a backup copy of your files.

First, run 2to3 in "soft mode" to see what it would actually do:

$ 2to3 /path/to/your/project

If you're happy with what it would do, you can then run 2to3 "for real":

$ 2to3 --write --nobackups /path/to/your/project

And now you have properly run 2to3 :)

aude
  • 789
  • 7
  • 15
7

On Windows:

python {path_to_python}\tools\scripts\2to3.py --output-dir={output_dir} -W -n {input_dir}

path_to_python = directory where Python is installed

output_dir = directory where to output the Python3 scripts

input_dir = directory from where to read the Python2 scripts

Avery
  • 2,035
  • 4
  • 33
  • 32
szdrnja
  • 134
  • 1
  • 5
4

To convert all python 2 files in a directory to 3, you simply could run $ C:\Program Files\Python\Tools\Scripts\2to3.py -w -n. inside the directory that you want to translate. It would skip all the non .py files anyway, and convert the rest.
note: remove the -n flag, if you want the backup file too.

Nuhman
  • 941
  • 14
  • 19
  • 1
    If you remove the -w flag, the file does not get converted at all. Removing this flag acts like preview of your potential changes. – Leonardo Lopez Mar 27 '18 at 21:58
  • @Leonardo Lopez, you're right. It was not `-w` but `-n`, that is used to set backup feature off. Thanks. – Nuhman Mar 28 '18 at 05:51
0

First install python 2to3 package :

C:\Default> pip install 2to3

Than convert your python2 file into python3 in your new folder i.e. python3-version/mycode

C:\Default> 2to3 your_file_name.py --output-dir=python3-version/mycode -w -n

Your new python3 file can be seen in new folder i.e. python3-version/mycode

Shivam Bharadwaj
  • 1,130
  • 14
  • 17
0

To convert the code from python2 to python3 first install the 2to3 package by using

pip install 2to3

Then run this command in directory where is your python code

2to3 -w -n .
  • -w flag to enable writeback, which applies the changes to the file
  • -n to disable backups
Sohaib
  • 490
  • 3
  • 12
0

Running it is very simple! I am going to consider you already have it installed and explain step-by-step how to proceed after that:

  1. Open terminal (or cmd for win users) inside the main folder containing the files you want to convert

e.g. C:\Users\{your_username}\Desktop\python2folder

  1. Type

python {your_2to3.py_install_directory} -w .\

e.g. in my case (win10) it would be:

python C:"\Program Files"\Python39\Tools\scripts\2to3.py -w .\

This is going to make the program scan the entire directory (and sub directories as well) and automatically convert everything that is written in Python2 to Python3.

-w flag makes the script apply the changes creating new converted files. So remove this you'd like to just scan and see what needs conversion (but without actually doing anything)

If you'd like to convert just one file instead of entire folders simply substitute .\ for python2_file_name.py:

e.g. python {your_2to3.py directory} -w python2_file_name.py

Also, by default it creates a .bak file for everything it converts. It is highly advised to keep it this way since any conversion is prone to errors but if you'd like to disable the automatic backup you could also add the -n flag.

e.g. python C:"\Program Files"\Python39\Tools\scripts\2to3.py -w -n python2_file_name.py

3.Done!

luzclasil
  • 33
  • 3
-2

The python 2to3.py file is mostly found in the directory C:/Program Files/Python/Tools/scripts if you already have python installed. I have python 3.6 and 2to3 is in the directory C:/Program Files/Python36/Tools/scripts. To convert a certain python 2 code to python 3, go to your command promt, change the directory to C:/Program Files/Python36/Tools/scripts where the 2to3 file is found. Then add the following command: python 2to3.py -w (directory to your script).

eg. C:\Program Files\Python36\Tools\scripts> python 2to3.py -w C:Users\Iykes\desktop\test.py.

the '-w' here ensures a backup file for your file is created.

  • 1
    If you remove the -w flag, the file does not get converted at all. Removing this flag acts like preview of your potential changes – Leonardo Lopez Mar 27 '18 at 21:58