简体   繁体   中英

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:

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)

Thank you for your help.

python manage.py makemigrations posts

Finally I found a solution.

Delete the content of app.py

and add blog.posts ( <sitename>.<app_name> ) in INSTALLED_APPS array inside settings.py file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM