FileTransporter错误UnboundLocalError:在assignmen之前引用了局部变量“classname”

2024-06-01 20:36:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用fileconvayor表单https://github.com/wimleers/fileconveyor

但是当我运行脚本时,我得到了以下错误。在

Traceback (most recent call last):
File "arbitrator.py", line 1185, in <module>
run_file_conveyor()
File "arbitrator.py", line 1168, in run_file_conveyor
arbitrator = Arbitrator(os.path.join(FILE_CONVEYOR_PATH, "config.xml"), rest
art)
File "arbitrator.py", line 142, in __init__
transporter_class = self._import_transporter(transporter_name)
File "arbitrator.py", line 1162, in _import_transporter
self.logger.error("The Transporter module '%s' was found, but its Transporte
r class '%s' could not be found."  % (module_name, classname))
UnboundLocalError: local variable 'classname' referenced before assignment
[root@af-server fileconveyor]#

这是来自仲裁员.py142号线

^{pr2}$

以及1162行的代码

transporter_class = None
    module = None
    alternatives = [transporter]
    default_prefix = 'transporters.transporter_' # Not  'fileconveyor.transporters.transporter_'!
    if not transporter.startswith(default_prefix):
        alternatives.append('%s%s' % (default_prefix, transporter))
    for module_name in alternatives:
        try:
            module = __import__(module_name, globals(), locals(), ["TRANSPORTER_CLASS"], -1)
        except ImportError:
            pass
    if not module:
        msg = "The transporter module '%s' could not be found." % transporter
        if len(alternatives) > 1:
            msg = '%s Tried (%s)' % (msg, ', '.join(alternatives))
        self.logger.error(msg)
    else:
        try:
            classname = module.TRANSPORTER_CLASS
            module = __import__(module_name, globals(), locals(), [classname])
            transporter_class = getattr(module, classname)
        except AttributeError:
            self.logger.error("The Transporter module '%s' was found, but its Transporter class '%s' could not be found."  % (module_name, classname))
    return transporter_class

这是文件中的全部代码

http://pastebin.com/ctsLrckq


Tags: nameinpyimportselflinenotclass
3条回答

这条线:

classname = module.TRANSPORTER_CLASS

引发AttributeError异常,因为module没有TRANSPORTER_CLASS属性。这会导致解释器在定义self.logger.error(...)变量之前跳到self.logger.error(...)。当记录器行尝试使用未定义的变量时,格式化日志字符串失败。为了修复它,您可以将记录器线路替换为以下线路:

^{pr2}$

请注意,__import__可以引发ImportError,这是不捕获的。我想这段代码的作者是想抓住它。在

当我按照指南安装FileTransporter时,也遇到了同样的错误安装.txt. 似乎有一些未解决的依赖关系。尝试用以下方法解决:

pip install django-cumulus==1.0.10

例外情况是捕捉到一个比您想象的早两行的错误:它出现在classname = module.TRANSPORTER_CLASS中。无论module是什么,它没有TRANSPORTER\u类attribute, so an exception is raised andclassname`从未定义。如果你没有吞下实际的错误消息,那就很清楚了。在

但我必须说,这个代码没有什么意义。在那个else子句中,您已经有了module,那么为什么要在下面的行中重新导入它?在

相关问题 更多 >