编译Python错误

编译Python错误,python,compilation,Python,Compilation,我有一段来自Python 2.4的代码,希望在Python 2.6上编译此代码,但我看到了以下错误: >>> py_compile.compile("Model.py") SyntaxError: ('invalid syntax', ('pcbModel.py', 73, 19, ' msg = "%s: %s. The error occurred generating \'%s\'." % (sys.exc_type, sys.exc_value, bName)\n')

我有一段来自Python 2.4的代码,希望在Python 2.6上编译此代码,但我看到了以下错误:

>>> py_compile.compile("Model.py")

SyntaxError: ('invalid syntax', ('pcbModel.py', 73, 19, ' msg = "%s: %s. The error occurred generating \'%s\'." % (sys.exc_type, sys.exc_value, bName)\n'))
代码是:

        try:
            pcb.Create(self.skipMeshing, analysisType = self.analysisType)
        msg = "%s: %s. The error occurred generating '%s'." % (sys.exc_type, sys.exc_value, bName)
        raise Exception, msg

        continue
    self.deactivate(bName)

如何解决它?

看起来您有一个
try
子句,除了之外没有相应的
。另外,
raisetype,args
表单不推荐使用,请使用
raisetype(args)
。另外,
sys.exc_type
和friends不是线程安全的。语法正确的版本是:

# DON'T DO THIS, SEE BELOW
try:
    pcb.Create(self.skipMeshing, analysisType = self.analysisType)
except Exception as e:
    msg = "%s: %s. The error occurred generating '%s'." % (type(e), e, bName)
    raise Exception(msg)
但是,看起来好像您正试图“捕获”异常,计算某种错误消息并引发异常。例外情况已经做到了这一点。上面的idomatic版本实际上是

pcb.Create(self.skipMeshing, analysisType = self.analysisType)
没有
尝试
,没有
除了
和没有
升起
。如果
pcb.Create()
引发异常,则表示它已引发异常,您无需再引发另一个异常