Edit This post has been edited as I have zeroed in on the issue but I have not yet resolved it.
Hello,
I am having an issue which throws the following error when I go to run tests through pycharm:
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
It appears the issue is with pycharm as I just ran ./manage.py test
and the tests executed successfully.
Configurations which I have set using pycharm are:
- Script path:/MainFolder/MainProjects/project1-backend/tests/test_models.py
- Environment Variables: PYTHONUNBUFFERED=1; DJANGO_SETTINGS_MODULE=project1backend.settings note the folder to which this belongs to, not project1-backend
- Python interpreter: Python 3.8 (MainFolder) ~/MainFolder/MainProjects/bin/python
- Working Directory:/Users/myname/MainFolder/MainProjects/project1-backend
- Add content roots to PYTHONPATH: not selected
- Add source roots to PYTHONPATH: selected
Project hierarchy
edit test file should be __init__.py
not __inity__.py
my .env file is located in the project1-backend folder. I am operating in a venv to execute ./manage.py test.
#import from Model class
from django.test import SimpleTestCase
from django.db import models
#User model
class User(models.Model):
email = models.CharField(max_length=256, primary_key=True)
first_name = models.CharField(max_length=256)
last_name = models.CharField(max_length=256)
def __str__(self):
return self.email # this defaults to the email address
from django.test import TestCase
from app.models import User
class TestModels(TestCase):
def setUp(self):
self.user1 = User.objects.create(
email="m@gmail.com",
first_name="misha",
last_name="lee"
)
def test_app_user(self):
self.setUp()
self.assertTrue(isinstance(self, User)
Edit
Traceback (most recent call last):
File "/Users/myname/MainFolder/MainProjects/project1-backend/tests/test_models.py", line 4, in <module>
from app.models import User
File "/Users/myname/MainFolder/MainProjects/project1-backend/app/models.py", line 6, in <module>
class User(models.Model):
File "/Users/myname/MainFolder/MainProjects/lib/python3.8/site-packages/django/db/models/base.py", line 107, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/myname/MainFolder/MainProjects/lib/python3.8/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
self.check_apps_ready()
File "/Users/myname/MainFolder/MainProjects/lib/python3.8/site-packages/django/apps/registry.py", line 134, in check_apps_ready
settings.INSTALLED_APPS
File "/Users/myname/MainFolder/MainProjects/lib/python3.8/site-packages/django/conf/__init__.py", line 76, in __getattr__
self._setup(name)
File "/Users/myname/MainFolder/MainProjects/lib/python3.8/site-packages/django/conf/__init__.py", line 57, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Process finished with exit code 1
I have also included
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'app.apps.AppConfig'
]
*note I have tried 'app' by itself as well to no avail.
Appreciate the help in advance! Note I have already looked at the PYTHONPATH answers as well as the __init__.py
ones and found they didn't help. I do however think it is path related.
I have also looked at this resource: Running Django tests in PyCharm but have found the answer didn't resolve my issue.