简体   繁体   中英

How to convert a Python library from Python2 to Python3

I have a code using "PyOPC" library ( https://github.com/ibh-systems/pyopc ). I fixed all the print statements by adding () at the starting and ending.

But when I install the library, I am getting more errors.

Such as:

  File "c:\anaconda3\lib\site-packages\PyOPC-0.1-py3.7.egg\PyOPC\servers\esdsrv.py", line 90
    def Read(self,(IPH,inOptions,outOptions)):
                  ^
SyntaxError: invalid syntax



  File "c:\anaconda3\lib\site-packages\PyOPC-0.1-py3.7.egg\PyOPC\utils.py", line 313
    def print_options((ilist,Options)):
                      ^
SyntaxError: invalid syntax


  File "c:\anaconda3\lib\site-packages\PyOPC-0.1-py3.7.egg\PyOPC\XDAClient.py", line 46
    except ZSI.FaultException, z:
                             ^
SyntaxError: invalid syntax


  File "c:\anaconda3\lib\site-packages\PyOPC-0.1-py3.7.egg\PyOPC\OPCContainers.py", line 257
    raise AttributeError,'Unknown complex type %s for filling'%buf
                        ^
SyntaxError: invalid syntax

Any easy way to fix them? I listed down four main errors, can someone tell me the correct syntax in Python3?

Parameter unpacking has been removed... things like

def foo(x, (y, z)):
    ...

should be changed to

def foo(x, _yz):
    (y, z) = _yz
    ...

This online tool may help https://www.pythonconverter.com/ which is based on https://docs.python.org/2/library/2to3.html

Edit:

Some Changes

1) print function syntax has been changed from print "Message" to print("Message")
2) xrange is replaced with range
3) Exception raising syntax was raise IOError, "file error" is now raise IOError("file error")
4) Exception Handling was

except NameError, err:
    print err, '--> our error message'

is now

except NameError as err:
    print(err, '--> our error message')

5) my_generator.next() is replaced with next(my_generator)
6) input() now always returns a string

more changes can be found on https://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html

def Read(self,IPH_and_inOptions_and_outOptions):
except ZSI.FaultException as z:
raise AttributeError('Unknown complex type %s for filling'%buf)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM