ImportError: cannot import name Poll

django, python, python-import

Solution

If you happen to be taking the official tutorial there might be the case that you changed the version tutorial at some point. Keep in mind that each version of the tutorial can dither slightly.

For example in the tutorial for v1.7 it's:

from polls.models import Question, Choice

For v1.6 it's

from polls.models import Poll, Choice

If you switched the version in the middle of the tutorial, then you should check your models.py file and see what names your classes have. Import those class names. So for example if you have

from django.db import models

class MyQuestion(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class MyChoice(models.Model):
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

Then you should import accordingly:

>>> from polls.models import MyQuestion, MyChoice

Problem

I have followed all the steps in this tutorial but I received this error. How can it be fixed? ``` dyn-72-33-214-65:mysite mona$ python manage.py shell Python 2.7.5 (default, Sep 2 2013, 05:24:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from polls.models import Poll, Choice Traceback (most recent call last): File "<console>", line 1, in <module> ImportError: cannot import name Poll ``` Directory structure is as follows: ``` >>> quit() dyn-72-33-214-65:mysite mona$ pwd /Users/mona/data_mining/mysite dyn-72-33-214-65:mysite mona$ ls db.sqlite3 manage.py mysite polls dyn-72-33-214-65:mysite mona$ cd polls/ dyn-72-33-214-65:polls mona$ ls __init__.py admin.py models.pyc views.py __init__.pyc models.py tests.py dyn-72-33-214-65:polls mona$ cd ../mysite/ dyn-72-33-214-65:mysite mona$ ls __init__.py settings.py urls.py __init__.pyc settings.pyc wsgi.py ``` Answer: Missed to add these lines in models.py from django.db import models ``` class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) ```

Original source