ImportError: Failed to import test module:

Cflux picture Cflux · Aug 3, 2018 · Viewed 21.2k times · Source

im having an issue with a tutorial im following. we have gotten to the point of testing and i continue to get an error when running

python manage.py test

Here is my error:

(restapi) chrismaltez@Chriss-MacBook-Pro:~/Desktop/pycharmprojects/UDEMY/restapi/src$ python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..E
======================================================================
ERROR: src.status.tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: src.status.tests
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 428, in _find_test_path
    module = self._get_module_from_name(name)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
    __import__(name)
  File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/restapi/src/status/tests.py", line 6, in <module>
    from .models import Status
  File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/restapi/src/status/models.py", line 20, in <module>
    class Status(models.Model): #fb status, instagram post, tween, linkedin post
  File "/Users/chrismaltez/Desktop/pycharmprojects/UDEMY/restapi/lib/python3.6/site-packages/django/db/models/base.py", line 118, in __new__
    "INSTALLED_APPS." % (module, name)
RuntimeError: Model class src.status.models.Status doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.


----------------------------------------------------------------------
Ran 3 tests in 0.096s

I dont understand why i get this error because i have status in the installed apps here:

INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',

        #third party
        'rest_framework',

        #local
        'accounts',
        'status',
        'updates',
    ]

and then here is my models that its saying i am having an error with:

from django.conf import settings
from django.db import models


def upload_status_image(instance, filename):
    return "status/{user}/{filename}".format(user=instance.user, filename=filename)


class StatusQuerySet(models.QuerySet):
    pass

class StatusManager(models.Manager):
    def get_queryset(self):
        return StatusQuerySet(self.model, using=self._db)


# Create your models here.
class Status(models.Model): #fb status, instagram post, tween, linkedin post
    user            = models.ForeignKey(settings.AUTH_USER_MODEL) # user instance .save
    content         = models.TextField(null=True, blank=True)
    image           = models.ImageField(upload_to=upload_status_image, null=True, blank=True) # great 3rd part package = django storanges --> AWS#pip install pillow to handle images
    updated         = models.DateTimeField(auto_now=True)
    timestamp       = models.DateTimeField(auto_now_add=True)

    objects = StatusManager

    def __str__(self):
        return str(self.content)[:50]

    class Meta:
        verbose_name = "Status post"
        verbose_name_plural = "Status posts"


    @property
    def owner(self):
        return self.user

i'm running on a Mac in django 1.11.8.

When i run makemigrations, i get no errors. when i runserver, i get no errors. i've tried this solution but nothing has worked.

Any and all help is appreciated.

-update- filetree:

restapi
|-bin
|-include
|-lib
|-scripts
|-src
  |-accounts
  |  |-api
  |  |  |-user
  |  |  |  |-__init__.py
  |  |  |  |-serializers.py
  |  |  |  |-urls.py
  |  |  |  |-views.py
  |  |  |-__init__.py
  |  |  |-permissions.py
  |  |  |-serializers.py
  |  |  |-test_user_api.py
  |  |  |-urls.py
  |  |  |-utils.py
  |  |  |-views.py
  |  |-+migrations
  |  |-__init__.py
  |  |-admin.py
  |  |-apps.py
  |  |-models.py
  |  |-tests.py
  |  |-views.py
  |-cfeapi
  |  |-restconf
  |  |  |-__init__.py
  |  |  |-main.py
  |  |  |-pagination.py
  |  |-__init__.py
  |  |-mixins.py
  |  |-settings.py
  |  |-urls.py
  |  |-wsgi.py
  |-status
    |-api    
    |  |-__init__.py
    |  |-serializers.py
    |  |-urls.py
    |  |-views.py
    |+migrations
    |-__init__.py
    |-admin.py
    |-apps.py
    |-forms.py
    |-models.py
    |-tests.py
    |-views.py

Answer

Cflux picture Cflux · Aug 4, 2018

i figured it out after more crawling on stackO. It was a simple fix I got the idea from here: website w/ answer . it turns out the answer was just to change the relative import to an abolute import

In status/tests.py, the line of code had to change from:

from .models import Status

to:

from status.models import Status

I still find it weird that it threw an error but the accounts/tests.py is fine.