django Why my app is not find (ModuleNotFoundError: No module named '< app_name>')?

8aqjt8rx  于 2022-11-26  发布在  Go
关注(0)|答案(1)|浏览(209)

我尝试用django rest framwork为我的博客创建一个API,当我执行以下命令时:

python manage.py makemigrations posts

出现此错误:

Traceback (most recent call last):
  File "/Users/xxx/dev/api/blog/blog/../manage.py", line 22, in <module>
    main()
  File "/Users/xxx/dev/api/blog/blog/../manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/xxx/dev/api/blog/env/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/Users/xxx/dev/api/blog/env/lib/python3.9/site-packages/django/core/management/__init__.py", line 420, in execute
    django.setup()
  File "/Users/xxx/dev/api/blog/env/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/xxx/dev/api/blog/env/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/Users/xxx/dev/api/blog/env/lib/python3.9/site-packages/django/apps/config.py", line 178, in create
    mod = import_module(mod_path)
  File "/usr/local/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'posts'

settings.py

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "rest_framework",
    "posts.apps.PostConfig",
]

apps.py
第一个
我已经检查了关于这个问题的问题的答案,但我什么也没找到。
像这样的问题:(模块错误)
谢谢你的帮助。

python manage.py makemigrations posts
2izufjch

2izufjch1#

最后我找到了解决办法。
删除app.py的内容
并将INSTALLED_APPS数组中的blog.posts<sitename>.<app_name>)添加到settings.py文件中

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "rest_framework",
    "blog.posts",
]

相关问题