Python Django设置模块异常

Python Django设置模块异常,python,django,exception,environment-variables,Python,Django,Exception,Environment Variables,我得到了以下错误: Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1741, in <module> main() File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1

我得到了以下错误:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1741, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\pydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Apache24/htdocs/consult/accounts/models.py", line 7, in <module>
    from django.contrib.auth.models import AbstractUser
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\db\models\base.py", line 87, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\apps\registry.py", line 249, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\apps\registry.py", line 131, in check_apps_ready
    settings.INSTALLED_APPS
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\conf\__init__.py", line 57, in __getattr__
    self._setup(name)
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\conf\__init__.py", line 42, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
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.

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.1\helpers\pydev\_pydevd_bundle\pydevd_xml.py", line 179, in _get_type
    if isinstance(o, t[0]):
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\utils\functional.py", line 213, in inner
    self._setup()
  File "C:\Apache24\htdocs\consult\clinicenv\lib\site-packages\django\conf\__init__.py", line 42, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested settings, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
]

中间件= [

]

ROOT_URLCONF='consult.url'

模板= [

]

WSGI_APPLICATION='consult.WSGI.APPLICATION'

数据库=

{

}

设置时验证用户\u MODEL=“accounts.USER”

wsgi.py

检查它是否指向正确的设置路径:

setdefault(“DJANGO\u SETTINGS\u MODULE”、“path/to/SETTINGS.py”)

尝试以下方法:

  • 如上所述,确保
    wsgi.py

    setdefault(“DJANGO\u SETTINGS\u MODULE”,“/path/to/SETTINGS.py”)

  • 确保上述路径正确,或者上述行中提到的设置文件是实际设置文件

  • 由于您正在使用Pycharm,上述错误可能是因为Pycharm环境变量出现了一些问题。尝试
    python manage.py flush

  • 如果问题仍然存在,请查看Pycharm设置中的环境配置,并检查是否存在任何差异


  • 要打开带有Django环境集的python终端,请运行

    python manage.py shell
    
    然后,例如,可以使用create\u user()helper函数创建一个新用户:

    >>> from django.contrib.auth.models import User
    >>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
    
    # At this point, user is a User object that has already been saved
    # to the database. You can continue to change its attributes
    # if you want to change other fields.
    >>> user.last_name = 'Lennon'
    >>> user.save()
    
    下面的示例代码显示了如何从脚本添加用户。请注意,您需要将第2行上的
    'project.settings'
    替换为您的项目名称

    project。在项目文件中设置指定已安装应用程序、中间件、模板等的点

    import os
    os.environ.setdefault('DJANGO_SETTINGS_MODULE','project.settings')
    
    import django
    django.setup()
    
    from django.contrib.auth.models import User
    
    super_user = User.objects.create_user(username = 'john',
                                    first_name = 'John',
                                    last_name = 'Doe',
                                    email = 'john.doe@mail.com',
                                    password = 'test',
                                    is_staff = True,
                                    is_superuser=True
                                    )
    

    文档中的更多信息

    如果您希望得到答案,您必须提供更多的上下文-更具体地说,您是如何得到此错误的。您也可以发布您的设置文件吗?
     'default': {
         'ENGINE': 'django.db.backends.mysql',
         'NAME': 'suitecaredb',
         'USER': 'root',
         'PASSWORD': 'innovations',
         'HOST': 'localhost',
         'PORT': '3306',
         'OPTIONS': {
            'charset': 'utf8mb4',
        },
               'TEST': {
            'CHARSET': 'utf8mb4',
            'COLLATION': 'utf8mb4_unicode_ci',
        }
     }
    
    python manage.py shell
    
    >>> from django.contrib.auth.models import User
    >>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
    
    # At this point, user is a User object that has already been saved
    # to the database. You can continue to change its attributes
    # if you want to change other fields.
    >>> user.last_name = 'Lennon'
    >>> user.save()
    
    import os
    os.environ.setdefault('DJANGO_SETTINGS_MODULE','project.settings')
    
    import django
    django.setup()
    
    from django.contrib.auth.models import User
    
    super_user = User.objects.create_user(username = 'john',
                                    first_name = 'John',
                                    last_name = 'Doe',
                                    email = 'john.doe@mail.com',
                                    password = 'test',
                                    is_staff = True,
                                    is_superuser=True
                                    )