[英]Fileconveyor Error - UnboundLocalError: local variable 'classname' referenced before assignment
I am using fileconvayor form https://github.com/wimleers/fileconveyor 我正在使用fileconvayor形式https://github.com/wimleers/fileconveyor
but when I run the script I get the following error. 但是当我运行脚本时,出现以下错误。
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]#
Here is the code from arbitrator.py line 142 这是来自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")
and the code from line 1162 和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
Here is whole code form that file 这是该文件的完整代码形式
I got the same error when I installed fileconveyor followed the guide INSTALL.txt. 当我按照指南INSTALL.txt安装fileconveyor时,出现了相同的错误。 It looks like there are some unresolved dependencies. 似乎有一些未解决的依赖项。 Try solve it with: 尝试用以下方法解决它:
pip install django-cumulus==1.0.10
The exception is catching an error two lines earlier than you thought: it is coming in the classname = module.TRANSPORTER_CLASS
. 异常比您想象的早两行捕获了一个错误:它出现在classname = module.TRANSPORTER_CLASS
。 Whatever module
is, it doesn't have a TRANSPORTER_CLASS attribute, so an exception is raised and
classname` is never defined. 无论module
是什么,它都没有TRANSPORTER_CLASS attribute, so an exception is raised and
永远不会定义classname。 If you weren't swallowing the actual error message, that would be clear. 如果您没有吞下实际的错误消息,那将会很清楚。
I must say though that this code makes very little sense. 我必须说,尽管这段代码没有什么意义。 In that else
clause you already have module
, so why are you reimporting it on the following line? 在else
子句中,您已经有了module
,那么为什么要在下一行重新导入它呢?
This line: 这行:
classname = module.TRANSPORTER_CLASS
throws an AttributeError
exception, because module
doesn't have a TRANSPORTER_CLASS
attribute. 抛出AttributeError
异常,因为module
没有TRANSPORTER_CLASS
属性。 This causes the interpreter to jump to the self.logger.error(...)
line before the classname
variable is defined. 这将导致解释器在定义classname
变量之前跳到self.logger.error(...)
行。 Formatting the log string fails when the logger line tries to use the undefined variable. 当记录器行尝试使用未定义的变量时,格式化日志字符串将失败。 In order to fix it, you could replace the logger line with this one: 为了解决此问题,您可以使用以下命令替换记录器行:
self.logger.error("The Transporter module '%s' was found, but its Transporter class could not be found." % (module_name,))
Please note that __import__
can raise ImportError
, which is not catched. 请注意__import__
可以引发ImportError
,但不会捕获。 I guess the author of this code meant to catch it. 我想这段代码的作者打算抓住它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.