Search code examples
pythonpython-2to3

Automated Python translation 2to3 error


I want to translate Python2 code to Python 3.It is very simple,but it does not work

import sys
import MySQLdb
import Cookbook

try:
 conn = Cookbook.connect ()
print "Connected"
 except MySQLdb.Error, e:
 print "Cannot connect to server"
 print "Error code:", e.args[0]
 print "Error message:", e.args[1]
 sys.exit (1)

conn.close ()
print "Disconnected"

I got this in terminal

2to3 harness.py
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Can't parse harness.py: ParseError: bad input: type=1, value='print', context=('', (9, 0))
RefactoringTool: No files need to be modified.
RefactoringTool: There was 1 error:
RefactoringTool: Can't parse harness.py: ParseError: bad input: type=1, value='print', context=('', (9, 0))

Why?


Solution

  • Don't know if this will solve your problem, but you could try fixing your indentation:

    import sys
    import MySQLdb
    import Cookbook
    
    try:
        conn = Cookbook.connect ()
        print "Connected"
    except MySQLdb.Error, e:
        print "Cannot connect to server"
        print "Error code:", e.args[0]
        print "Error message:", e.args[1]
        sys.exit (1)
    
    conn.close ()
    print "Disconnected"