[英]Why my app is not find (ModuleNotFoundError: No module named '<app_name>')?
I'm trying to create an API for my blog with django rest framwork and when I execute the following command:我正在尝试使用 django rest 框架为我的博客创建一个 API,当我执行以下命令时:
python manage.py makemigrations posts
This Error appears:出现此错误:
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
from django.apps import AppConfig
class PostConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "posts"
.
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-39.pyc
│ ├── settings.cpython-39.pyc
│ └── urls.cpython-39.pyc
├── asgi.py
├── posts
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── serializers.py
│ ├── tests.py
│ └── views.py
├── settings.py
├── urls.py
└── wsgi.py
I already check the answer of the questions that deals with this problem but I found nothing.我已经检查了处理此问题的问题的答案,但一无所获。
Like this question: [https://stackoverflow.com/questions/57833395/modulenotfounderror-no-module-named-posts-in-django](Module Error)喜欢这个问题:[https://stackoverflow.com/questions/57833395/modulenotfounderror-no-module-named-posts-in-django](模块错误)
Thank you for your help.谢谢您的帮助。
python manage.py makemigrations posts
Finally I found a solution.最后我找到了解决办法。
Delete the content of app.py
删除
app.py
的内容
and add blog.posts
( <sitename>.<app_name>
) in INSTALLED_APPS
array inside settings.py
file并在
settings.py
文件中的INSTALLED_APPS
数组中添加blog.posts
( <sitename>.<app_name>
)
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework",
"blog.posts",
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.