Search code examples
pythondjangoopensuse

UnboundLocalError: local variable 'full_path' referenced before assignment while syncdb


There's a similar question but which is not answering in the particular case I found today.

I'm running on openSUSE, and have this cryptic error when doing a syncdb. I'm posting my findings to help others in a similar situation.

> 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

Solution

  • The error is not pretty (should have been caught, and a complete error message should have been returned to the user), but this happens when some default or optional serialization modules are not present on your system.

    For example, if in your settings.py, in the SERIALIZATION_MODULES dict, the wadofstuff.django.serializers.json serialization module is mentioned, and it is not installed, it will return that error.

    Install it by, for example:

    pip install wadofstuff-django-serializers
    

    On openSUSE, another possibility is that the python-xml module is not installed (it is not part of the python-base package), and so it is possible that the import of xml.sax.saxutils fails.

    In that case, install python-xml, for example by doing:

    sudo zypper in python-xml
    

    and it should work.

    In case this doesn't work, you can run pdb to put a breakpoint where it is failing. For example:

    > 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('.')
    

    At some point, you would find the error popping up clearly:

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