'How to Access Settings file from .py file - DRF
This is my project structure. I am trying to access settings.py file from lookups.py
I am importing using following code,
import os
import sys
import django
sys.path.append('/path/to/django/project')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'manageemployee.settings')
django.setup()
from manageemployee.apps.employee.hrmmongodb import DBMixin
from manageemployee.settings import EVENT_STORE_DICT
But I am getting the following error,
ModuleNotFoundError: No module named 'manageemployee'
Solution 1:[1]
It's better to use settings by importing the object django.conf.settings
wherever it was needed instead of the settings file itself as you are trying,
Example:
from django.conf import settings
if settings.DEBUG:
# Do Something
So in your use case:
from django.conf import settings
EVENT_STORE_DICT = settings.EVENT_STORE_DICT
# Do Something
Note that django.conf.settings
isn’t a module – it’s an object. So
importing individual settings is not possible:
from django.conf.settings import DEBUG # This won't work.
You can also find the detailed information in the Docs
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Faisal Nazik |