Cryptic errors when using django-nose

07 December 2011   2 comments   Django

Powered by Fusion×

After about 3 days of debugging using pdb, print and writing to a log file I've almost finally solve my bizarre errors I was getting when running a whole test suite. The error that it lead to was that Django refused to re-register models to the admin and the errors looked something like this:

 File "/Users/peterbe/dev/MOZILLA/PTO/pto/", line 6, in <module>
 File "/Users/peterbe/dev/MOZILLA/PTO/pto/vendor/src/django/django/contrib/admin/", line 26, in autodiscover
   import_module('%s.admin' % app)
 File "/Users/peterbe/dev/MOZILLA/PTO/pto/vendor/src/django/django/utils/", line 35, in import_module
 File "/Users/peterbe/dev/MOZILLA/PTO/pto/apps/users/", line 30, in <module>, UserProfileAdmin)
 File "/Users/peterbe/dev/MOZILLA/PTO/pto/vendor/src/django/django/contrib/admin/", line 85, in register
   raise AlreadyRegistered('The model %s is already registered' % model.__name__)
AlreadyRegistered: The model UserProfile is already registered

Turns out to be independent of which Django project I ran and it was something no one else was able to reproduce on any machine with the exact same code.

After 2 days I found that there's a difference between a successful run and a failing run was how I specified (to nose) which module to load:

./ test users  # fails!
./ test users.test  # works!

In both cases it finds the same tests. So it would either fail 10 times or work 10 times. Hmmm...

The bridging between nose and Django is done by awesome django-nose developed here at Mozilla by Django extraordinaire Jeff Balogh and it's a non-trivial piece of code as it depends on some really smart importing tricks and stuff which I haven't even begun to understand.

However, after so many trial and errors I finally discovered that the solution (for me) was to delete the ~/.noserc file. What's strange is that all it contained was:


I might never actually find out what went wrong. Ultimately I think a reason things went wrong was because it incorrectly populated sys.modules with excessive keys that would cause double imports of which in turn runs admin.autodiscover() but incorrectly does so twice.

Sorry for the rambling. And sorry for not actually finding the real bug. I did spent 2-3 days debugging this non-stop and hopefully some other poor frustrated person is going to see this and also look into the ~/.noserc for ways to fix it maybe.


MichaƂ Pasternak
I have the same thing now. Importing admin before, with the same syntax like the parameter passed to import lib does not do antyhing.
I have the same problem but there is no ".noserc" in my home folder (by using the default test runner all works fine... what the hell nose runner does???)

Your email will never ever be published

Related posts

Python file with closing automatically 03 December 2011
Persistent caching with fire-and-forget updates 14 December 2011
Related by keywords:
To code or to pdb in Python 20 December 2010
Trivial but powerful tips for nosetests 19 November 2011