ImportError: Failed to import test module:
Solution 1
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()
Solution 2
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.
Solution 3
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.
Cflux
By day: architectural designer By night: learning to code. hobbies: too many to list.
Updated on July 09, 2022Comments
-
Cflux almost 2 years
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
-
Abhay Maniyar almost 4 yearsI am also facing this error but doing the opposite to what you did here solved my issue. Idk why(very new to django).
-
Patrizio Bertoni about 3 yearsGot the same scenario but problem persists :(