# ImportError/ModuleNotFoundError

**URL:** <https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324>\
**Category:** Getting Started\
**Created:** [May 9, 2020, 2:28am UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324 "2020-05-09T02:28:54Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![amorestperpe](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/amorestperpe/32/1180_2.png) [@amorestperpe](https://forum.djangoproject.com/u/amorestperpe)\
**Post date:** [May 9, 2020, 2:28am UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/1 "2020-05-09T02:28:54Z")

</div>

Hi, I’m trying to work through the tutorial. I’m currently on the testing section and when I go to run the test:  
python manage.py test polls

I get the following error:

# System check identified no issues (0 silenced). E

## ERROR: mysite.polls (unittest.loader.\_FailedTest)

ImportError: Failed to import test module: mysite.polls  
Traceback (most recent call last):  
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py”, line 470, in \_find\_test\_path  
package = self.\_get\_module\_from\_name(name)  
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py”, line 377, in \_get\_module\_from\_name  
**import** (name)  
ModuleNotFoundError: No module named ‘mysite.polls’

* * *

Ran 1 test in 0.000s

FAILED (errors=1)

I’m not really sure what’s going on I’ve been trying to figure it out and research it for the past few hours.

Thanks for the help

---

<div class="post-metadata">

**Author:** ![jlgimeno](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jlgimeno/32/284_2.png) [@jlgimeno](https://forum.djangoproject.com/u/jlgimeno)\
**Post date:** [May 9, 2020, 2:41am UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/2 "2020-05-09T02:41:53Z")

</div>

Are you able to show us your directory structure? Use dir in the command prompt (or equivalent) in Windows, or ls (Linux and Mac) so we can see what you have.

-Jorge

---

<div class="post-metadata">

**Author:** ![amorestperpe](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/amorestperpe/32/1180_2.png) [@amorestperpe](https://forum.djangoproject.com/u/amorestperpe)\
**Post date:** [May 9, 2020, 2:55am UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/3 "2020-05-09T02:55:49Z")

</div>

I’m working with a Mac  
 ![Tree](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/4/4561a0c92cd44b0a4f7964a06a75866ab5859d4e.png)

---

<div class="post-metadata">

**Author:** ![jlgimeno](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jlgimeno/32/284_2.png) [@jlgimeno](https://forum.djangoproject.com/u/jlgimeno)\
**Post date:** [May 9, 2020, 3:09am UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/4 "2020-05-09T03:09:09Z")

</div>

I have the feeling that having the top level folder (named mysite) and the project folder (also named mysite but the path is mysite/mysite) may be confusing the test runner. I would try renaming the top level mysite to something else (maybe mysite\_root). Then try again.

-Jorge

---

<div class="post-metadata">

**Author:** ![amorestperpe](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/amorestperpe/32/1180_2.png) [@amorestperpe](https://forum.djangoproject.com/u/amorestperpe)\
**Post date:** [May 9, 2020, 3:15am UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/5 "2020-05-09T03:15:17Z")

</div>

I was thinking the same thing. So, I changed the outer mysite and got a similar error which is posted below.

I was under the impress that this mysite (shell)/mysite (inner) was just normal Django convention?

# System check identified no issues (0 silenced). E

## ERROR: mysite\_root.polls.tests (unittest.loader.\_FailedTest)

ImportError: Failed to import test module: mysite\_root.polls.tests  
Traceback (most recent call last):  
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py”, line 436, in \_find\_test\_path  
module = self.\_get\_module\_from\_name(name)  
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py”, line 377, in \_get\_module\_from\_name  
**import** (name)  
File “/Users/dijon.henderson/PycharmProjects/learn\_django/mysite\_root/polls/tests.py”, line 6, in   
from .models import Question  
File “/Users/dijon.henderson/PycharmProjects/learn\_django/mysite\_root/polls/models.py”, line 16, in   
class Question(models.Model):  
File “/Users/dijon.henderson/PycharmProjects/learn\_django/venv/lib/python3.8/site-packages/django/db/models/base.py”, line 112, in **new**  
raise RuntimeError(  
RuntimeError: Model class mysite\_root.polls.models.Question doesn’t declare an explicit app\_label and isn’t in an application in INSTALLED\_APPS.

* * *

Ran 1 test in 0.013s

FAILED (errors=1)

---

<div class="post-metadata">

**Author:** ![jlgimeno](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jlgimeno/32/284_2.png) [@jlgimeno](https://forum.djangoproject.com/u/jlgimeno)\
**Post date:** [May 9, 2020, 3:19am UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/6 "2020-05-09T03:19:10Z")

</div>

Can you post your settings.py please?

-J

---

<div class="post-metadata">

**Author:** ![amorestperpe](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/amorestperpe/32/1180_2.png) [@amorestperpe](https://forum.djangoproject.com/u/amorestperpe)\
**Post date:** [May 9, 2020, 3:24am UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/7 "2020-05-09T03:24:18Z")

</div>

import os

# Build paths inside the project like this: os.path.join(BASE\_DIR, …)

BASE\_DIR = os.path.dirname(os.path.dirname(os.path.abspath( **file** )))

# SECURITY WARNING: don’t run with debug turned on in production!

DEBUG = True

ALLOWED\_HOSTS = []

# Application definition

INSTALLED\_APPS = [  
‘polls.apps.PollsConfig’,  
‘django.contrib.admin’,  
‘django.contrib.auth’,  
‘django.contrib.contenttypes’,  
‘django.contrib.sessions’,  
‘django.contrib.messages’,  
‘django.contrib.staticfiles’,  
]

MIDDLEWARE = [  
‘django.middleware.security.SecurityMiddleware’,  
‘django.contrib.sessions.middleware.SessionMiddleware’,  
‘django.middleware.common.CommonMiddleware’,  
‘django.middleware.csrf.CsrfViewMiddleware’,  
‘django.contrib.auth.middleware.AuthenticationMiddleware’,  
‘django.contrib.messages.middleware.MessageMiddleware’,  
‘django.middleware.clickjacking.XFrameOptionsMiddleware’,  
]

ROOT\_URLCONF = ‘mysite.urls’

TEMPLATES = [  
{  
‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,  
‘DIRS’: [],  
‘APP\_DIRS’: True,  
‘OPTIONS’: {  
‘context\_processors’: [  
‘django.template.context\_processors.debug’,  
‘django.template.context\_processors.request’,  
‘django.contrib.auth.context\_processors.auth’,  
‘django.contrib.messages.context\_processors.messages’,  
],  
},  
},  
]

WSGI\_APPLICATION = ‘mysite.wsgi.application’

# Database

DATABASES = {  
‘default’: {  
‘ENGINE’: ‘django.db.backends.sqlite3’,  
‘NAME’: os.path.join(BASE\_DIR, ‘db.sqlite3’),  
}  
}

# Password validation

AUTH\_PASSWORD\_VALIDATORS = [  
{  
‘NAME’: ‘django.contrib.auth.password\_validation.UserAttributeSimilarityValidator’,  
},  
{  
‘NAME’: ‘django.contrib.auth.password\_validation.MinimumLengthValidator’,  
},  
{  
‘NAME’: ‘django.contrib.auth.password\_validation.CommonPasswordValidator’,  
},  
{  
‘NAME’: ‘django.contrib.auth.password\_validation.NumericPasswordValidator’,  
},  
]

# Internationalization

LANGUAGE\_CODE = ‘en-us’

TIME\_ZONE = ‘America/New\_York’

USE\_I18N = True

USE\_L10N = True

USE\_TZ = True

# Static files (CSS, JavaScript, Images)

STATIC\_URL = ‘/static/’

---

<div class="post-metadata">

**Author:** ![jlgimeno](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jlgimeno/32/284_2.png) [@jlgimeno](https://forum.djangoproject.com/u/jlgimeno)\
**Post date:** [May 9, 2020, 4:21am UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/8 "2020-05-09T04:21:16Z")

</div>

Sorry for the delay. In trying to troubleshoot this, I found I had a broken Python 3.8 installation. Now fixed. Yay!

I tried this steps exactly as in the tutorial (I have done the tutorial a couple of times in the past, and this time in the interest of time I copied and pasted. I don’t recommend it while one is learning). I got the test to run using the top level folder of “mysite”. So, my original premise was incorrect.

Can you please post your tests.py, please?

-J

---

<div class="post-metadata">

**Author:** ![amorestperpe](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/amorestperpe/32/1180_2.png) [@amorestperpe](https://forum.djangoproject.com/u/amorestperpe)\
**Post date:** [May 9, 2020, 4:58am UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/9 "2020-05-09T04:58:53Z")

</div>

Lol. Crazy, well thanks so far. See below for my tests.py

import datetime

from django.test import TestCase  
from django.utils import timezone

from .models import Question

class QuestionModelTests(TestCase):

```
def test_was_published_recently_with_future_question(self):
    """
    was_published_recently() returns False for questions whose pub_date
    is in the future.
    """
    time = timezone.now() + datetime.timedelta(days=30)
    future_question = Question(pub_date=time)
    self.assertIs(future_question.was_published_recently(), False)
```

---

<div class="post-metadata">

**Author:** ![jlgimeno](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jlgimeno/32/284_2.png) [@jlgimeno](https://forum.djangoproject.com/u/jlgimeno)\
**Post date:** [May 9, 2020, 2:30pm UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/10 "2020-05-09T14:30:50Z")

</div>

This certainly has been a bit of a journey there and back again (minus the ring and Mordor). May I ask if running the development server works without errors (python manage.py runserver)?

---

<div class="post-metadata">

**Author:** ![amorestperpe](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/amorestperpe/32/1180_2.png) [@amorestperpe](https://forum.djangoproject.com/u/amorestperpe)\
**Post date:** [May 9, 2020, 2:46pm UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/11 "2020-05-09T14:46:16Z")

</div>

Yes, the development server runs without issue.

(venv) My-MacBook-Pro:mysite Me$ python manage.py runserver  
Watching for file changes with StatReloader  
Performing system checks…

System check identified no issues (0 silenced).  
May 09, 2020 - 10:43:04  
Django version 3.0.6, using settings ‘mysite.settings’  
Starting development server at [http://127.0.0.1:8000/](http://127.0.0.1:8000/)  
Quit the server with CONTROL-C.

---

<div class="post-metadata">

**Author:** ![amorestperpe](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/amorestperpe/32/1180_2.png) [@amorestperpe](https://forum.djangoproject.com/u/amorestperpe)\
**Post date:** [May 9, 2020, 3:50pm UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/12 "2020-05-09T15:50:49Z")

</div>

So, I did the same as you. I started over from scratch and the new test suite is running fine, the old is still broken and I’m still not sure what is going on with that. 😕

---

<div class="post-metadata">

**Author:** ![jlgimeno](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jlgimeno/32/284_2.png) [@jlgimeno](https://forum.djangoproject.com/u/jlgimeno)\
**Post date:** [May 9, 2020, 3:57pm UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/13 "2020-05-09T15:57:41Z")

</div>

Something is different. I just don’t know what that is. I would say that it’s best to continue now that you have things working again. Sorry I couldn’t pinpoint what the problem was!

-Jorge

---

<div class="post-metadata">

**Author:** ![amorestperpe](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/amorestperpe/32/1180_2.png) [@amorestperpe](https://forum.djangoproject.com/u/amorestperpe)\
**Post date:** [May 9, 2020, 4:12pm UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/14 "2020-05-09T16:12:26Z")

</div>

It’s fine. I greatly appreciate the attempt and the help. I’m just crossing my fingers every time I run a test, hoping that Django runs it without error lmao. I’m new to this whole thing —programming, that is — and I’ve been trying out the TDD approach. It helps me stay focused and on task… So if I can’t run test, I’m DOA lmao.

Thanks.

---

<div class="post-metadata">

**Author:** ![arboc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/arboc/32/583_2.png) [@arboc](https://forum.djangoproject.com/u/arboc)\
**Post date:** [May 10, 2020, 9:07pm UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/15 "2020-05-10T21:07:49Z")

</div>

@amorestperpe  
If you’d like to become more confident about what’s happening and revisit the “broken” project, I found something that might explain the issues you’re experiencing.

Look at the second answer in this SO thread, from user ‘Samuel’: [https://stackoverflow.com/questions/40206569/django-model-doesnt-declare-an-explicit-app-label](https://stackoverflow.com/questions/40206569/django-model-doesnt-declare-an-explicit-app-label)

He describes that he experienced a similar issue to what you’re getting as long as he had an ` __init__.py` file in his project/root directory. I didn’t catch it first in the screenshot in your second post here, but you have the same thing. There’s an ` __init__.py` file right above the `db.sqlite3` and `manage.py` files.

I’m also working my way through the official tutorial (again). I just tried adding a ` __init__.py` file to the root directory and bam, running `python manage.py test` produces ‘ImportError: Failed to import test module: official\_tutorial.polls’. If I remove (`rm __init__.py`) the file and run `python manage.py test` again, everything’s back to normal.

I don’t know what exactly Django does in the background which causes this behavior, but apparently it’s really confused by the root directory being turned into a package (which is what ` __init__.py` [does](https://docs.python.org/3/reference/import.html#regular-packages)).

---

<div class="post-metadata">

**Author:** ![jlgimeno](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jlgimeno/32/284_2.png) [@jlgimeno](https://forum.djangoproject.com/u/jlgimeno)\
**Post date:** [May 10, 2020, 9:24pm UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/16 "2020-05-10T21:24:06Z")

</div>

Good catch! When I get a chance I’m going to try and figure out why that is.

-Jorge

---

<div class="post-metadata">

**Author:** ![Oridan](https://avatars.discourse-cdn.com/v4/letter/o/fbc32d/32.png) [@Oridan](https://forum.djangoproject.com/u/Oridan)\
**Post date:** [August 13, 2020, 8:03pm UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/17 "2020-08-13T20:03:15Z")

</div>

I had the same issue and deleting **init**.py ended up solving it, thanks!

---

<div class="post-metadata">

**Author:** ![jaquatrezz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jaquatrezz/32/4050_2.png) [@jaquatrezz](https://forum.djangoproject.com/u/jaquatrezz)\
**Post date:** [March 28, 2021, 10:28pm UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/18 "2021-03-28T22:28:33Z")

</div>

Really helpful. Thanks for that! We had the same issue

---

<div class="post-metadata">

**Author:** ![j0lv3r4](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/j0lv3r4/32/8517_2.png) [@j0lv3r4](https://forum.djangoproject.com/u/j0lv3r4)\
**Post date:** [May 14, 2022, 5:55pm UTC](https://forum.djangoproject.com/t/importerror-modulenotfounderror/2324/19 "2022-05-14T17:55:04Z")

</div>

The ` __init__.py` file is most likely needed. What would be a solution without deleting the file? Is this a bug?
