21

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
Cflux
  • 1,423
  • 3
  • 19
  • 39
  • Are you sure your PyCharm points to the right root directory? https://stackoverflow.com/questions/34304044/pycharm-current-working-directory?rq=1 – dmitryro Aug 03 '18 at 16:18
  • 2
    You have `'status'` (without the `src` prefix) in your `INSTALLED_APPS`, therefore it should be importing the model as `status.models.Status`, but the error shows `src.status.models.Status`. If you show the layout of the files in your project it might indicate what the problem is. If there is an `__init__.py` in the `src` directory, then try removing it. – Alasdair Aug 03 '18 at 16:24
  • Add a tree view of your project directory structure. – Sachin Aug 03 '18 at 16:30
  • is there an easy way to copy the tree view from pycharm besides taking a snippet? everytime i make a project tree for stack overflow, it takes a decent amount of time. i just did a quick search but didnt see anything promising. – Cflux Aug 03 '18 at 17:43
  • took a while but i added the tree view – Cflux Aug 04 '18 at 17:41
  • @Alasdair i tried removing the __init__.py file and running the tests but it still gave me the same error. – Cflux Aug 04 '18 at 17:56
  • 1
    You can use the `tree` command to display the project layout. On a Mac, you can install it using `brew`. – Alasdair Aug 05 '18 at 18:55
  • @Alasdair, just got it working. its a huge time saver. Thank you! – Cflux Aug 06 '18 at 18:50

5 Answers5

10

I ran into this issue where I got the ModuleNotFoundError for a folder in my path that I wasn't directly specifying.

I was running my test file inside PyCharm.

The fix was adding the main entry point to my test file:

if __name__ == '__main__':
    unittest.main()
rmooney
  • 6,123
  • 3
  • 29
  • 29
6

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.

Cflux
  • 1,423
  • 3
  • 19
  • 39
2

Also the test package should not have the same name as the source package. Eg:

- src
 - utils
  - utilities.py
- tests
 - utils
  - test_utilities.py

This setup can cause the error, although it depends on your PYTHONPATH and where you run the tests from. Note how there's utils package in both src and test directories. Renaming one of the packages seems to solve the problem, eg: utils and test_utils.

Not directly the situation that OP is facing, but I thought it's worth mentioning.

Voy
  • 5,286
  • 1
  • 49
  • 59
0

Try such as python manage.py test --pattern="*_models.py"

Lane
  • 4,682
  • 1
  • 36
  • 20
0

Double check you pycharm root directory also, if you did put your virtual environment to the same directory as django apps, move env file to higher hierarchy