简体   繁体   中英

How to run a python2 program in python3?

I am trying to run a program I wrote using Python2 program in Python3. I am trying to make any necessary changes to the syntax for it to work but I am stuck on the following line of code.

raise AttributeError, fn + ' is not a search function in search.py.'

It works fine when I run it in Python2 but I keep getting the following syntax error when trying to run it in Python3.

Traceback (most recent call last):
  File "autograder.py", line 345, in <module>
    moduleDict[moduleName] = loadModuleFile(moduleName, os.path.join(options.codeRoot, cp))
  File "autograder.py", line 134, in loadModuleFile
    return imp.load_module(moduleName, f, "%s.py" % moduleName, (".py", "r", imp.PY_SOURCE))
  File "/usr/lib/python3.6/imp.py", line 235, in load_module
    return load_source(name, filename, file)
  File "/usr/lib/python3.6/imp.py", line 172, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 684, in _load
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 674, in exec_module
  File "<frozen importlib._bootstrap_external>", line 781, in get_code
  File "<frozen importlib._bootstrap_external>", line 741, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "searchAgents.py", line 71
    raise AttributeError, fn + ' is not a search function in search.py.'
                        ^
SyntaxError: invalid syntax

This syntax for raising exceptions was removed from python 3

You'll need to change your exception code to something like:

raise AttributeError(fn + ' is not a search function in search.py.')

The answer provided in this question goes into much more detail - Manually raising (throwing) an exception in Python

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