Django出现ModuleNotFoundError,无法识别应用程序的名称

时间:2018-11-25 17:25:46

标签: python django django-rest-framework

每当我尝试运行Django命令(makemigrationsmigrateshellrunserver)时,都会遇到相同的异常。我看了其他帖子,但没有任何帮助。我更改了应用程序的命名,但这也没有用。异常回溯是:

    Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f3c3ff9dc80>
Traceback (most recent call last):
  File "/home/usr/projects/myproject/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/home/usr/projects/myproject/env/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/home/usr/projects/myproject/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "/home/usr/projects/myproject/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "/home/usr/projects/myproject/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/home/usr/projects/myproject/env/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/usr/projects/myproject/env/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
    app_config = AppConfig.create(entry)
  File "/home/usr/projects/myproject/env/lib/python3.6/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/home/usr/projects/myproject/env/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'api'

我的apps.py是:

from django.apps import AppConfig

class ApiConfig(AppConfig):
    name = 'api'
    verbose_name = 'Project API'

,在我的__init__.py上的主要项目中,我有:

default_app_config = 'api.apps.ApiConfig'

我的INSTALLED_APPS是:

INSTALLED_APPS = [
    'api',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'rest_framework',
    'api.apps.ApiConfig',
]

如果这有所作为,我将使用2.1.3版的Django。据我所知,问题涉及INSTALLED_APPS,因为每当我更改时,找不到的模块都会更改为我添加的内容。

结构:

-myproject
  -api
    django app files
  -settings.py
  -init
  -urls
  -wsgi.py

1 个答案:

答案 0 :(得分:1)

看起来您的应用程序目录位于项目目录中。实际上,它需要是一个目录,位于项目目录的旁边

将其从myproject/api/移至api/,从INSTALLED_APPS中删除其中一项,它应该可以工作。