5

I am trying out the Django tutorial on the djangoproject.com website, but when I reach the part where I do the first "makemigrations polls" I keep getting this error:

ImportError: No module named apps

 Traceback (most recent call last): File "manage.py", line 22, in execute_from_command_line(sys.argv) File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line utility.execute() File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 312, in execute django.setup() File "/Library/Python/2.7/site-packages/django/__init__.py", line 18, in setup apps.populate(settings.INSTALLED_APPS) File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 85, in populate app_config = AppConfig.create(entry) File "/Library/Python/2.7/site-packages/django/apps/config.py", line 112, in create mod = import_module(mod_path) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) 

How can I resolve this error?

6
  • What version of Django do you have installed? Commented Sep 16, 2016 at 3:18
  • I get this when I type python -m django --version : /usr/bin/python: No module named django.__main__; 'django' is a package and cannot be directly executed Commented Sep 16, 2016 at 3:21
  • Be sure you install django correctly. Commented Sep 16, 2016 at 3:30
  • show us your catalog structure, and your settings.py file Commented Sep 16, 2016 at 7:40
  • uninstall and Install Django properly. Commented Sep 16, 2016 at 8:08

6 Answers 6

7

Your problem is that your Django version does not match the version of the tutorial.

In Django 1.9+, the startapp command automatically creates an app config class, so the tutorial asks you to add polls.apps.PollsConfig to INSTALLED_APPS.

For Django 1.8 and earlier, the tutorial asks you to add polls to INSTALLED_APPS. If you add polls.apps.PollsConfig instead, you will get an import error, unless you create the PollsConfig manually.

Sign up to request clarification or add additional context in comments.

Comments

5

There is an error in the tutorial.

It instructs to add polls.apps.PollsConfig in the INSTALLED_APPS section of the settings.py file. I changed it from polls.apps.PollsConfig to simply polls and that did the trick. I was able to successfully make migrations.

I hope this helps other people who face similar problems.

2 Comments

I don't think there's an error in the tutorial, the problem is that you were following the wrong version of the tutorial.
Any idea why this solution worked, because it worked for me too.
5

I had a really similar issue, but one that was definitely different than the OP. That said, this post was one of the first responses I found when debugging my issue, so I'm hijacking it with an answer to my question.

Problem

The app I was building had apps nested beneath a parent namespace, e.g. customapp.polls instead of just polls. The error I saw was

ModuleNotFoundError: No module named 'polls' 

but probably looks like the following if you're on Python 3.5 or older:

ImportError: No module named polls 

Note that this says polls instead of apps in the original post.

Answers from this post

  • @Alasdair's answer is a good one to try. If I force a similar issue, I get an error about customapp.polls.apps.PollsConfig and customapp.polls.apps being missing instead of apps, but that could just be differences in versions.
  • @Monil's answer "solved" my issue, even though I had a PollsConfig subclass defined. As @Alasdair suggests, the reason that works is often because you didn't add an AppConfig subclass.

Solution

In my case, the error complained about the polls module being missing (not apps). And since @Monils answer "solved" my issue, I was able to narrow it down to my actual configuration. My config looked equivalent to:

class PollsConfig(AppConfig): name = 'polls' 

but since I put my apps underneath a parent module, I should have written:

class PollsConfig(AppConfig): name = 'customapp.polls' 

1 Comment

THIS. Works for Django 2.0+. Should be noted that the entry in customapp/settings.py should have the installed app named also changed from 'polls' to 'customapp.polls.' Thanks!
1

You need to install required packages in your virtualenv to run Django project. First and foremost create virtualenv for your project.

virtualenv env #For python 2.7 virtualenv -p python3 env #For python 3.4 

Actiavte env to install your requirements.

source env/bin/activate 

By using pip you can then install your packages.

pip install Django 

And then start your Django project.

Comments

0

In Django 1.10.6 I had the same error ("no module named..."). The solution that worked for me is changing "polls.apps.PollsConfig" for "mysite.polls" in settings.py. o.O

Comments

0

I found the solution for me. When you write polls.apps.PollsConfig in your InstalledApps, you need to have in mind that, the first polls refers to the created app, not to the site. In the Django documentation it can be a bit confusing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.