UnboundLocalError:在同步数据库中分配之前引用了局部变量“full_path”

Posted

技术标签:

【中文标题】UnboundLocalError:在同步数据库中分配之前引用了局部变量“full_path”【英文标题】:UnboundLocalError: local variable 'full_path' referenced before assignment while syncdb 【发布时间】:2013-02-15 08:48:26 【问题描述】:

有一个similar question,但在我今天发现的特定情况下没有回答。

我在 openSUSE 上运行,并且在执行 syncdb 时遇到了这个神秘的错误。我发布我的发现是为了帮助处于类似情况的其他人。

> python manage.py syncdb
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py",     line 459, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py",     line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
    return self.handle_noargs(**options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 164, in handle_noargs
    call_command('loaddata', 'initial_data', verbosity=verbosity, database=db)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 150, in call_command
    return klass.execute(*args, **defaults)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 239, in handle
    (full_path, ''.join(traceback.format_exception(sys.exc_type,
UnboundLocalError: local variable 'full_path' referenced before assignment

【问题讨论】:

【参考方案1】:

错误并不漂亮(应该已经被捕获,并且应该向用户返回完整的错误消息),但是当您的系统上不存在某些默认或可选的序列化模块时会发生这种情况。

例如,如果在您的settings.py中,SERIALIZATION_MODULES dict中提到了wadofstuff.django.serializers.json序列化模块,并且没有安装它,它将返回该错误。

安装它,例如:

pip install wadofstuff-django-serializers

在 openSUSE 上,另一种可能是python-xml 模块没有安装(它不是python-base 包的一部分),因此xml.sax.saxutils 的导入可能会失败。

在这种情况下,安装python-xml,例如:

sudo zypper in python-xml

它应该可以工作。

如果这不起作用,您可以运行pdb 在失败的地方设置断点。例如:

> python -m pdb manage.py syncdb
> manage.py(2)<module>() -> from django.core.management import execute_manager
(Pdb) b /usr/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py:110
Breakpoint 1 at /usr/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py:110
(Pdb) c
Creating tables ...
Installing custom SQL ...
Installing indexes ...
> /usr/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py(110)handle()
-> for fixture_label in fixture_labels:
(Pdb) n
> /usr/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py(111)handle()
-> parts = fixture_label.split('.')

在某些时候,您会清楚地发现错误弹出:

ImportError: 'No module named xml.sax.saxutils'

【讨论】:

以上是关于UnboundLocalError:在同步数据库中分配之前引用了局部变量“full_path”的主要内容,如果未能解决你的问题,请参考以下文章

Python UnboundLocalError 异常

UnboundLocalError:赋值前引用的局部变量()

不明白为啥会发生 UnboundLocalError(关闭)[重复]

不明白为啥会发生 UnboundLocalError(关闭)[重复]

UnboundLocalError:分配前引用的局部变量“公会”

UnboundLocalError:分配前引用的局部变量“光标”