0

I want to import 2to3.py in the shell, but it keeps saying SyntaxError.

>>> site.addsitedir('/Python27/Tools/Scripts/')
>>> sys.path
['C:\\Senti\\ltpservice', 'C:\\Python27\\Lib\\idlelib', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\Tools\\Scripts']
>>> import 2to3
SyntaxError: invalid syntax

What should I do?

TerryA
  • 58,805
  • 11
  • 114
  • 143
user2870222
  • 269
  • 1
  • 3
  • 13
  • 1
    who says that the module is called "2to3"? This is the name of the generated script. And modules can not start with a numeric character. –  Oct 11 '13 at 08:43
  • 1
    the library behind `2to3` is called [`lib2to3`](http://docs.python.org/2/library/2to3#module-lib2to3) - but there shouldn't be much reason for using it from within a python program. Also, it's API is unstable. – mata Oct 11 '13 at 08:50

2 Answers2

1

You're trying to import something starting with a number:

>>> import 1sdjkhl
  File "<stdin>", line 1
    import 1sdjkhl
           ^
SyntaxError: invalid syntax
>>> 

2to3 isn't a module, it's a tool. You run it like this in the shell:

$ 2to3 myscript.py
TerryA
  • 58,805
  • 11
  • 114
  • 143
  • thx, but I still have a problem. in the cmd line, I still have a SyntaxError. – user2870222 Oct 11 '13 at 08:56
  • $^ 2to3 example1.py SyntaxError: invalid syntax – user2870222 Oct 11 '13 at 08:57
  • @user2870222 Not in the interactive interpreter, in the shell (i.e the place where you type `python`) – TerryA Oct 11 '13 at 09:01
  • in cmd line I get the result I wanted, "python 2to3.py -w c:\python27\example.py". However, I still don't know how to call 2to3 in the Python Shell. do I need to simply type "$ 2to3 example1.py" without importing any other modules? – user2870222 Oct 11 '13 at 09:22
  • 1
    @user2870222 Just type `2to3 example1.py` the `$` I put in my answer was there to show that You type it in the shell – TerryA Oct 11 '13 at 09:30
1

To use 2to3, all you to do if you are a windows user is to go to- \python27\2.7\Tools\Scripts> on DOS command prompt. At the prompt, type: "python 2to3.py", "-w", then the directory where the file(s) is/are located. For example: C:\Users\Leeman\python27\2.7\Tools\Scripts>python 2to3.py c:/practice/py2folder/py2file.py. If you add 2to3.py to environment variables' path, the typing may be shorten to simply: python 2to3.py -w c:/practice/py2folder/py2file.py. And in case you want to convert entire folder and sub folders, you type: python 2to3.py -w c:/practice/py2folder/.