Django running py file reported an error

my directory structure is like this clipboard.png
db.pymodels
clipboard.png
models.py:


: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.

clipboard.png
and then the problem comes again. The following Times error message, this error is directly in the db.py file, import models. I wonder if it is because I introduced the models module automatically generated by django, which does not support running .py files directly, so there is a django test unit.

Traceback (most recent call last):
  File "C:/Users/malloc/Desktop/pythonprj/pycharmpj/Django/myblog/blog/db.py", line 3, in <module>
    import models
  File "C:\Users\malloc\Desktop\pythonprj\pycharmpj\Django\myblog\blog\models.py", line 4, in <module>
    class User(models.Model):
  File "C:\Appsofmy\Anaconda\lib\site-packages\django\db\models\base.py", line 87, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Appsofmy\Anaconda\lib\site-packages\django\apps\registry.py", line 249, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Appsofmy\Anaconda\lib\site-packages\django\apps\registry.py", line 131, in check_apps_ready
    settings.INSTALLED_APPS
  File "C:\Appsofmy\Anaconda\lib\site-packages\django\conf\__init__.py", line 57, in __getattr__
    self._setup(name)
  File "C:\Appsofmy\Anaconda\lib\site-packages\django\conf\__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Appsofmy\Anaconda\lib\site-packages\django\conf\__init__.py", line 107, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Appsofmy\Anaconda\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named "config"

what is the situation of asking? How to solve it? I really fainted

Jan.11,2022

before using a django component such as templates,models in a separate python script, you need to declare the DJANGO_SETTINGS_MODULES environment variable and then call django.setup () to load the application in the installed_apps variable in the settings file.
follow the prompt: first check to see if 'blog' is set in the list of INSTALLED_APPS in the settings file of the project.
then modify the code in db.py as follows:
import os
import django
os.environ ['DJANGO_SETTINGS_MODULE'] =' myblog.settings'
django.setup ()
import blog.models
print'.'


call the sys module to point to the path of config relative to the current py file

import sys
impost os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
os.environ['DJANGO_SETTINGS_MODULE'] = 'yourconfig.settings'
django.setup()

it seems that this can point to the relative path of config. The import of other BTW, modules must be followed by django.setup (), otherwise it will not take effect. Ha
I hope it will be helpful to you


https://luliangce.gitee.io/bl.

there is also relevant content in the official documents.

Menu