ModuleNotFoundError:没有名为“ __main __。models”的模块

时间:2019-05-18 09:10:06

标签: python django django-models

这是项目框架。 目录:用户。 testing.py和models.py在用户目录中。

对于以下代码,将产生上述错误:

from .models import CustomUser
username='Jason'
userLL = CustomUser.objects.get(username=username)
print(userLL)
last_login = userLL.last_login
print(last_login)

错误是

Traceback (most recent call last):
  File "C:/Users/lenovo/Desktop/vidyaConnect/users/testing.py", line 1, in <module>
    from .models import CustomUser
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package

如果我在模型之前删除点(。),则会出现以下错误

Traceback (most recent call last):
  File "C:/Users/lenovo/Desktop/vidyaConnect/users/testing.py", line 1, in <module>
    from models import CustomUser
  File "C:\Users\lenovo\Desktop\vidyaConnect\users\models.py", line 1, in <module>
    from django.contrib.auth.models import AbstractUser
  File "C:\Users\lenovo\Desktop\projectVC\venv\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:\Users\lenovo\Desktop\projectVC\venv\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "C:\Users\lenovo\Desktop\projectVC\venv\lib\site-packages\django\db\models\base.py", line 103, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Users\lenovo\Desktop\projectVC\venv\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Users\lenovo\Desktop\projectVC\venv\lib\site-packages\django\apps\registry.py", line 134, in check_apps_ready
    settings.INSTALLED_APPS
  File "C:\Users\lenovo\Desktop\projectVC\venv\lib\site-packages\django\conf\__init__.py", line 79, in __getattr__
    self._setup(name)
  File "C:\Users\lenovo\Desktop\projectVC\venv\lib\site-packages\django\conf\__init__.py", line 64, 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.

0 个答案:

没有答案