Django - ImportError: No module named apps

24,122

Solution 1

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.

Solution 2

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.

Solution 3

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'

Solution 4

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.

Share:
24,122
Monil
Author by

Monil

Updated on August 13, 2020

Comments

  • Monil
    Monil over 3 years

    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?