Debugging cryptic "Error: cannot import name <Name>" on Django - django

Sometimes when I run manage.py I get a cryptic message in red that says Error: cannot import name <Name> and nothing else.
Obviously, this is a simple import or syntax error and with a little looking around, I can usually fix it. Other times however it takes me quite a while to figure out what exactly I did wrong. Is there a way to get Django to spit out more info in this situation?

This is an annoying problem. Luckily, it's been fixed (recently): see this ticket and this patch.
If you want to just hack your local django install (you're running under virtualenv or buildout, right?), change to the root of your django directory (the one with README, INSTALL, etc) and run this:
curl "https://code.djangoproject.com/changeset/17197?format=diff&new=17197" | patch -p3\
So, if you run django trunk > 17197, apply the patch to your django install (it applied to django 1.2 for me), or wait until django 1.4, you'll be able to do this:
./manage.py shell --traceback
And you'll get a full traceback instead of just the Error: cannot import ...
Voila!

this happens when a circular import appears, when one model is dependent on another and in turn they try and import each other while executing code.
You might want to paste the two models that are causing you issues.
That'll help us debug.
Also it tends to happen sometimes with signals so if you have a signals file please paste too.

Related

Moving from Django 1.6.x to 1.9.x import model errors

I have a few defined apps in my Django project, each with their own sub-directory (created with startapp)
In the views.py of app1 I have an import to a model from app2
from app2.models import MyModel
This worked in Django 1.6.x. In version 1.9 I get:
Could not resolve variable
sometimes on MyModel, sometimes on the filter(..) method, or on both.
If I change the import to
from app2.models import * ##UnusedWildImport
then everything works just fine.
Has anything changed in 1.9.x (or before) that requires a different mode of importing models external to the app?
I think I can rule our circular import problems as this would have failed in 1.6...
Edit: Based on the comments I started wondering whether this might be a PyDev problem.
I tried:
Removing and re-adding Python to PyDev - it did not help
This https://stackoverflow.com/a/8534599/5958359 - removing the myproject/src folder from PYTHONPATH worked ... with a caveat.
The error did not appear when I completely removed the import statement, so this is not a good solution
This is a PyDev error.
Searches haven't yielded an adequate solution - most simply explain how to disable the error - so I will not link to any solution here.
My workaround, as much as I don't like from xxx import * seems like the best temporary solution.

django-selectable LookupAlreadyRegistered error

i am trying django-selectable the 'fruit' example given in the docs here -
https://django-selectable.readthedocs.org/en/version-0.6.2/quick-start.html#defining-a-lookup
getting LookupAlreadyRegistered error,there is a small note on this error,but i can't
figure out how to solve this.i understand that it has something to do with import statements, i am using django-1.3
i will provide more info if needed , but i am just using the same code as given in the example.
kindly help
Check the note in the link you've posted:
You should only register your lookup once. Attempting to register the same lookup class more than once will lead to LookupAlreadyRegistered errors. A common problem related to the LookupAlreadyRegistered error is related to inconsistant import paths in your project. Prior to Django 1.4 the default manage.py allows for importing both with and without the project name (i.e. from myproject.myapp import lookups or from myapp import lookups). This leads to the lookup.py file being imported twice and the registration code executing twice. Thankfully this is no longer the default in Django 1.4. Keeping your import consistant to include the project name (when your app is included inside the project directory) will avoid these errors.

Syntastic + Django

I just started developing on Django, and then I thought using the Syntastic syntax checker on it would be a good idea.
The problem is that it complains about some things being wrong when, in fact, they aren't.
Examples:
For
from django.core.urlresolvers import reverse
I get:
error| [F0401] Unable to import 'django.core.urlresolvers'
For
amount = self.sale_set.filter(date__year=year).aggregate(sum=Sum('amount'))["sum"]
I get (where self is an Album)
error| [E1101, Album.get_sales_total] Instance of 'Album' has no 'sale_set' member
This code runs perfectly even with these "errors", but how can I make Syntastic behave correctly?
piggybacking on #supervacuo's answer:
there is a way to get this working for syntastic and it's rather straightforward, if not easy to figure out for someone unfamiliar with syntastic options (like, say, me):
in your .vimrc, add this line:
let g:syntastic_python_pylint_args = "--load-plugins pylint_django"
of course, this does require pylint-django be installed in your environment
Both of these messages come from pylint — you can see fuller explanations with pylint --help-msg=$ID, or on http://pylint-messages.wikidot.com/.
You can disable the checks with e.g. from django.core.urlresolvers import reverse
# pylint: disable=F0401, but that gets tiresome pretty quickly.
There's a pylint plugin for Django which will definitely fix your E1101 (and I hope the F0401 too). Maybe have a go at installing the plugin and configuring Syntastic to use it?

Django incorrect import

I am developing a web app with django 1.2.4, but I am having a problem with the Site model. I try:
from django.contrib.sites.models import Site
...
if Site._meta.installed:
...
I am getting the error undefined variable from import: _meta in the if statement,
any help?
Unless you've fiddled with the django source, there really should be any problems with the Sites._meta.installed variable. _meta.installed is assigned from within the metaclass of all models (using contribute_to_class()) so it would affect ALL models if the code were broken.
A quick search for relevant tickets does not reveal such a problem for that version (or any other version) of django.
Are you by any chances running django via pydev? If so, perhaps this post is relevant: How do I fix PyDev "Undefined variable from import" errors?
That's of course a wild speculation on my part. If you can post a Trackback of your error, we might be able to get a better insight into your problem.
Response to comments:
"I get the error in the IDE (apatana Studio 3)"
Aptana uses PyDev and so will exhibit the same problem. Here's a possible fix taken from this blog post:
Open up Aptana Studio
Open Window > Preferences > PyDev > Editor > Code Analysis
Select the “Undefined” tab
Add DoesNotExist at the end of the “Consider the following names as globals” list
Apply and restart

import error when running django south

I added a ManyToMany field between my facebook user and car Reviews to ./facebook/model.py which required me to insert:
from car.models import Review
I try to run:
./manage.py schemamigration facebook --auto
but i get error:
django.core.exceptions.ImproperlyConfigured: ImportError haystack: cannot import name Review
The problem is, my facebook app has nothing to do with the third party haystack module. I tried some simple debugging and found as long as i try to import Review, i get the error. It doesn't matter if I change the model or not. Could it be the order of my "INSTALLED_APPS"? I have "car" followed by "facebook" and then "haystack".
So, a bit of background on how imports work: When you run a statement like "from x.y import z", the entire module x.y is executed, and then the interpreter pulls z and places it in your namespace.
So, your underlying problem probably has nothing to do with South or Haystack; it's probably in car.models somewhere. That's why you're getting an error no matter how you come across the importing of Review, and you'll likely get it if you try to import anything else from that module.
Check car.models for problems -- in particular, you might have a circular import (in other words, a case where A imports from B and B imports from A).
Or simply the model with its files does not exist in your environment but does exist in your configuration :)
(happened to me when I forgot to "git add" a folder and after building into staging got this error...)