How To Use 2to3 Properly For Python?
Solution 1:
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
Solution 2:
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.)
Solution 3:
It's important to have a backup before running
2to3
.
- If you're using git, make a commit.
- 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
:)
Solution 4:
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
Solution 5:
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.
Post a Comment for "How To Use 2to3 Properly For Python?"