32

I have installed python 32 package to the

C:\python32

I have also set the paths:

PYTHONPATH | C:\Python32\Lib;C:\Python32\DLLs;C:\Python32\Lib\lib-tk;

PATH ;C:\Python32;

I would like to use the "2to3" tool, but CMD does not recognize it.

CMD: c:\test\python> 2to3 test.py 

Should i add an extra path for "2to3" or something?

Thanks

3 Answers 3

50

2to3 is actually a Python script found in the Tools/scripts folder of your Python install.

So you should run it like this:

python.exe C:\Python32\Tools\scripts\2to3.py your-script-here.py 

See this for more details: http://docs.python.org/library/2to3.html

Sign up to request clarification or add additional context in comments.

4 Comments

Use the -w option to not only check what should change, but also write converted code: python.exe C:\Python32\Tools\scripts\2to3.py -w your-script-here.py
By adding C:\Python32\Tools\scripts` (adjusted to your Python installation of course) to your PATH, you can avoid typing the whole path everytime and just use 2to3.py -w your-script-here.py` from any directory.
Are you sure about that? By default Windows only recognizes .exe, .com, .bat files, I think for what you're saying you'd have to adjust PATHEXT as well, to include .PY in it.
For what @CGFoX was suggesting, you can see monknomo's answer below as a working alternative
11

You can set up 2to3.py to run as a command when you type 2to3 by creating a batch file in the same directory as your python.exe file (assuming that directory is already on your windows path - it doesn't have to be this directory it just is a convenient, relatively logical spot).

Lets assume you have python installed in C:\Python33. If you aren't sure where your python installation is, you can find out where Windows thinks it is by typing where python from the command line.

You should have python.exe in C:\Python33 and 2to3.py in C:\Python33\Tools\Scripts.

Create a batch file called 2to3.bat in C:\Python33\Scripts and put this line in the batch file

@python "%~dp0\..\Tools\Scripts\2to3.py" %* 

The %~dp0 is the location of the batch file, in this case c:\Python33\Scripts and the %* passes all arguments from the command line to the 2to3.py script. After you've saved the .bat file, you should be able to type 2to3 from the command line and see

At least one file or directory argument required. Use --help to show usage. 

I have found this technique useful when installing from setup.py, because sometimes the setup script expects 2to3 to be available as a command.

3 Comments

Great answer! That's approximately the same thing I've come up to myself. I'd only suggest adding @ to the .bat file line to suppress output and moving the file to C:\Python35\Scripts dir, as it is a better fit for files like that (for example pip.exe and easy_install are located there).
Those are all excellent refinements. I'll edit to reflect your suggestions
If you are using some newer python versions (3.11+?) and being annoyed by DeprecationWarning: lib2to3 package is deprecated and ..., use: @python -W ignore::DeprecationWarning "%~dp0\..\Tools\Scripts\2to3.py" %*
0

Make a batch file then rename it to 2to3.bat and paste this code in it:

@python "%~dp0\Tools\Scripts\2to3.py" %* 

Copy that file beside your python.exe file, for me that folder is: C:\Users\Admin\AppData\Local\Programs\Python\Python38

Usage:

2to3 mycode.py 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.