2to3 Help?

M

marco.m.petersen

I have Python 3.0. I tried to use the 2to3 program included with the
interpreter to convert some scripts for Python 2.5 to Python 3.0 ones.
When I try to start it form the Python command line, it says it is a
syntax error.

This was the line of code:

$ 2to3 testscript.py

Any help would be appreciated.

Thanks
- Marco
 
J

James Mills

I have Python 3.0. I tried to use the 2to3 program included with the
interpreter to convert some scripts for Python 2.5 to Python 3.0 ones.
When I try to start it form the Python command line, it says it is a
syntax error.

This was the line of code:

$ 2to3 testscript.py

Any help would be appreciated.

$ 2to3 testscript.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Can't open testscript.py: [Errno 2] No such file or
directory: 'testscript.py'
RefactoringTool: No files need to be modified.
RefactoringTool: There was 1 error:
RefactoringTool: Can't open testscript.py: [Errno 2] No such file or
directory: 'testscript.py'

Seems to work perfectly fine here.

It would greatly help if you showed us the error!

cheers
James
 
M

marco.m.petersen

I have Python 3.0. I tried to use the 2to3 program included with the
interpreter to convert some scripts for Python 2.5 to Python 3.0 ones.
When I try to start it form the Python command line, it says it is a
syntax error.
This was the line of code:
$ 2to3 testscript.py
Any help would be appreciated.

$ 2to3 testscript.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Can't open testscript.py: [Errno 2] No such file or
directory: 'testscript.py'
RefactoringTool: No files need to be modified.
RefactoringTool: There was 1 error:
RefactoringTool: Can't open testscript.py: [Errno 2] No such file or
directory: 'testscript.py'

Seems to work perfectly fine here.

It would greatly help if you showed us the error!

cheers
James

The error was:
File "<stdin>", line 1
$ 2to3.py testscript.py
^
Syntax Error: Invalid Syntax
 
J

James Mills

File "<stdin>", line 1
$ 2to3.py testscript.py
^
Syntax Error: Invalid Syntax

Oh i see...

You need to do 2 things:

1) Run 2to3 on the shell not the python interpreter.
2) Learn some basic UNIX.

cheers
James
 
M

marco.m.petersen

You need to do 2 things:
1) Run 2to3 on the shell not the python interpreter.
2) Learn some basic UNIX.

cheers
James

I tried to run it on the command prompt (I use Windows XP) but it
doesn't work either.

I opened the command prompt:

C:\Documents and Settings\User>

Then I typed '$ 2to3 testscript.py':

C:\Documents and Settings\User>$ 2to3.py testscript.py

But then this appears:

C:\Documents and Settings\User>$ 2to3.py testscript.py
'$' is not recognized as an internal or external command,
operable program or batch file.

Anyway I'll try some UNIX. Thanks.
 
J

James Mills

I tried to run it on the command prompt (I use Windows XP) but it
doesn't work either.

I did not realize you were using WIndows :)
Normally most shells in the UNIX/Linux world
start with a '$'.
I opened the command prompt:

C:\Documents and Settings\User>

Then I typed '$ 2to3 testscript.py':

C:\Documents and Settings\User>$ 2to3.py testscript.py

But then this appears:

C:\Documents and Settings\User>$ 2to3.py testscript.py
'$' is not recognized as an internal or external command,
operable program or batch file.

Your problem is, you are treating the '$' (most likely
copied/pasted from readings on the web) as part
of what you need to enter into the command prompt.

'$' is (like I said above) normally part of most commonly
configured shells (eg: Bash).

You need to just enter:

2to3 testscript.py
Anyway I'll try some UNIX. Thanks.

That is a good idea :) Windows teaches you nothing!

cheers
James
 
A

alex23

I tried to run it on the command prompt (I use Windows XP) but it
doesn't work either.
'$' is not recognized as an internal or external command,
operable program or batch file.

'$' is used in the instructions to denote a command prompt. You're not
supposed to type it :)

In your case '$' == 'C:\Documents and Settings\User>'; just type
'2to3.py testscript.py' and it should work (assuming it's location is
in your PATH and you have the right handlers set up to launch it)
 
M

marco.m.petersen

Oh okay. I don't think my Python installation is in PATH yet. I'll try
to add it then.
 
A

alex23

Oh okay. I don't think my Python installation is in PATH yet. I'll try
to add it then.

I don't yet have 3.0 installed, but my guess is you'll also need to
have 'C:\Python30\Scripts' in your path as well. I'm assuming that's
where it resides, so check first.
 
M

marco.m.petersen

I don't yet have 3.0 installed, but my guess is you'll also need to
have 'C:\Python30\Scripts' in your path as well. I'm assuming that's
where it resides, so check first.

Its in C:\Python30\Tools\Scripts.

Anyway it now works.

C:\Documents and Settings\User>python C:\Python30\Tools\Scripts
\2to3.py C:\Python30\testscript.py

Thanks a lot alex23.
 
J

John Machin

Oh okay. I don't think my Python installation is in PATH yet. I'll try
to add it then.

Am I permitted to ask a really silly question?
Thanks.
Here it is: How much working 2.X code do you have that needs migrating
from 2.x to 3.x?

Cheers,
John
 
T

Terry Reedy

I have Python 3.0. I tried to use the 2to3 program included with the
interpreter to convert some scripts for Python 2.5 to Python 3.0 ones.

I believe 2to3 is will work 'best' with files modified to use some new
2.6 features (backported from 3.0) where 'best' means 'Runs with 2.6 and
then with 3.0 after 2to3 conversion without hand editing'. If you want
to do a one-time conversion, then 2.5 to 3.0 is fine.
 
J

jjh

I have Python 3.0. I tried to use the 2to3 program included with the
interpreter to convert some scripts for Python 2.5 to Python 3.0 ones.
When I try to start it form the Python command line, it says it is a
syntax error.

This was the line of code:

$ 2to3 testscript.py

Any help would be appreciated.

Thanks
- Marco

Sorry to bring this back up. I'm a newbie trying to run 2to3 on
Windows. I ran the following:

C:\Python30\python.exe C:\Python30\Tools\Scripts\2to3.py c:\python\src
\testscript.py

and I got the following:

C:\Users\user>C:\Python30\python.exe C:\Python30\Tools\Scripts\2to3.py
c:\python\src\testscript.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
--- c:\python\src\testscript.py (original)
+++ c:\python\src\testscript.py (refactored)
@@ -1,3 +1,3 @@
#! /usr/bin/python

-print "Hello World";
+print("Hello World");
RefactoringTool: Files that need to be modified:
RefactoringTool: c:\python\src\testscript.py

what am I suppose to do after that? I thought it would re-write my
testscript.py but it didn't. Thanks so much for the help.
 
J

John Machin

On Jan 14, 10:01 pm, (e-mail address removed) wrote: [snipped]

Sorry to bring this back up.

The correct thing to do is to start a NEW post. Marco's problem is
nothing to do with yours.



I'm a newbie trying to run 2to3 on
Windows. I ran the following:

C:\Python30\python.exe C:\Python30\Tools\Scripts\2to3.py c:\python\src\testscript.py

The .exe should not be necessary ... if you have put a python.bat or a
python.com or similar in that directory, lose them soon.
and I got the following:

C:\Users\user>C:\Python30\python.exe C:\Python30\Tools\Scripts\2to3.py
c:\python\src\testscript.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
--- c:\python\src\testscript.py (original)
+++ c:\python\src\testscript.py (refactored)
@@ -1,3 +1,3 @@
 #! /usr/bin/python

-print "Hello World";
+print("Hello World");
RefactoringTool: Files that need to be modified:
RefactoringTool: c:\python\src\testscript.py

what am I suppose to do after that? I thought it would re-write my
testscript.py but it didn't. Thanks so much for the help.

What made you think so? Thinking is a very good idea, but when you are
a new user of a piece of software it's an even better idea to read the
manual (http://docs.python.org/3.0/library/2to3.html) and check that
it follows the usual command-line help convention (c:\python30\python
c:\python30\tools\scripts\2to3.py --help) and that the fancy Windows
help for Python is working ((Windows XP) click on Start>All
Programs>Python 3.0>Python Manuals, type 2to3 then Enter) just in case
you are ever disconnected from the Internet and/or want faster
response.

HTH,
John
 

vss

Joined
Dec 1, 2021
Messages
1
Reaction score
0
Tips:
1. 2to3 is actually a Python script found in the Tools/scripts folder of your Python install
2. 2to3 --help for help a t the command prompt after cd to directory where 2to3.exe is located
3. Documentation page: https://docs.python.org/3.0/library/2to3.html

From <https://stackoverflow.com/questions/10104805/python-2to3-windows-cmd>

Steps:

i) Install 2to3 package by launching Python and executing the following command:

pip install 2to3

Two ways to execute 2to3 and obtain the converted code

1. Using 2to3.exe:

a) Location of 2to3.exe:
C:\Users\svi3\AppData\Local\Continuum\anaconda3\pkgs\python-3.7.4-h5263a28_0\Scripts

b) Command executed in windows command prompt after changing directory to the one where 2to3.exe is located:
2to3 -w D:\users\svi3\kbArticles2Contribute\randomRespVrfEx\python3\prrforcHandcal4.py

2. Using 2to3.py

a. If the path to python.exe is not added to Windows System environment variable PATH, find the location of python.exe file. In this laptop, it is located in the folder C:\Users\svi3\AppData\Local\Continuum\anaconda3\pkgs\python-3.7.4-h5263a28_0

b. Once the python.exe file is found, place the 2to3.py file in the local direcotry where the .py files have to be converted and execute the following command:

C:\Users\svi3\AppData\Local\Continuum\anaconda3\pkgs\python-3.7.4-h5263a28_0\python 2to3.py -w D:\users\svi3\kbArticles2Contribute\randomRespVrfEx\python3\prrforcHandcal4.py
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,056
Messages
2,570,446
Members
47,097
Latest member
MarionMajo

Latest Threads

Top