How to fix “UnboundLocalError: local variable ‘full_path’ referenced before assignment” in Django 1.4

In Django 1.4, whenever I execute

python manage.py syncdb

This error appears after the creation of all the tables:

...
Installing custom SQL ...
Installing indexes ...
Traceback (most recent call last):
 File "manage.py", line 10, in <module>
 execute_from_command_line(sys.argv)
 File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
 utility.execute()
 File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute
 self.fetch_command(subcommand).run_from_argv(self.argv)
 File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 196, in run_from_argv
 self.execute(*args, **options.__dict__)
 File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 232, in execute
 output = self.handle(*args, **options)
 File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 371, in handle
 return self.handle_noargs(**options)
 File "/usr/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 164, in handle_noargs
 call_command('loaddata', 'initial_data', verbosity=verbosity, database=db)
 File "/usr/lib/python2.7/dist-packages/django/core/management/__init__.py", line 150, in call_command
 return klass.execute(*args, **defaults)
 File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 232, in execute
 output = self.handle(*args, **options)
 File "/usr/lib/python2.7/dist-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

In order to fix it, you must open the framework file django/core/management/commands/loaddata.py as root, go to line 239 and change the whole function call from this:

else:
self.stderr.write(
self.style.ERROR("Problem installing fixture '%s': %s\n" %
(full_path, ''.join(traceback.format_exception(sys.exc_type,
sys.exc_value, sys.exc_traceback)))))

To this:

else:
full_path = ""
self.stderr.write(
self.style.ERROR("Problem installing fixture '%s': %s\n" %
(full_path, ''.join(traceback.format_exception(sys.exc_type,
sys.exc_value, sys.exc_traceback)))))

The variable full_path is being used before being declared in that context, so you just declare is as an empty string and it will stop giving you trouble.

Apparently, this problem has already been fixed in in Django 1.5.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: