UnboundLocalError:在syncdb分配之前引用的局部变量'full_path'

时间:2013-02-15 08:48:27

标签: python django opensuse

有一个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 个答案:

答案 0 :(得分:2)

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

例如,如果在您的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'