Fileconveyor Error - UnboundLocalError:局部变量' classname'在分配之前引用

时间:2014-03-31 12:45:05

标签: python fileconveyor

我正在使用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]#

以下是来自arbitrator.py第142行的代码

   # Verify that all referenced transporters are available.
    transporters_not_found = 0
    for server in self.config.servers.keys():
        transporter_name = self.config.servers[server]["transporter"]
        transporter_class = self._import_transporter(transporter_name)
        if not transporter_class:
            transporters_not_found += 1
    if transporters_not_found > 0:
        raise TransporterAvailabilityTestError("Consult the log file for details")

和第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

3 个答案:

答案 0 :(得分:1)

当我按照指南INSTALL.txt安装fileconveyor时出现同样的错误。 看起来有一些未解决的依赖项。尝试解决它:

pip install django-cumulus==1.0.10

答案 1 :(得分:0)

异常是比你想象的早两行捕获一个错误:它来自classname = module.TRANSPORTER_CLASS。无论module是什么,它都没有定义TRANSPORTER_CLASS attribute, so an exception is raised and classname`。如果您没有吞下实际的错误消息,那就很清楚了。

我必须说这段代码没什么意义。在else条款中,您已经拥有module,那么为什么要在以下行重新导入它?

答案 2 :(得分:0)

这一行:

classname = module.TRANSPORTER_CLASS

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

self.logger.error("The Transporter module '%s' was found, but its Transporter class could not be found."  % (module_name,))

请注意,__import__可能会引发ImportError,而不是{{1}}。我想这段代码的作者意味着抓住它。