部署错误。运行WSGI应用程序时出错。ModuleNotFoundError: 未找到模块名 ‘api.urls’

huangapple go评论68阅读模式
英文:

Deployment error. Error running WSGI application. ModuleNotFoundError: No module named 'api.urls'

问题

To fix the "ModuleNotFoundError: No module named 'api.urls'" error, you need to ensure that your project structure and configurations are correctly set up. Here's a checklist of things to verify:

  1. Directory Structure:

    • Confirm that your project structure is consistent with Django conventions.
    • Check that the "api" app is located inside your main project directory, which seems to be "/home/danialsk/unitree/unitree" based on your WSGI file.
  2. Python Path:

    • Ensure that you've added the correct path to your project directory in your WSGI file. It appears to be correctly set in your provided code:

      path = '/home/danialsk/unitree/unitree'
      
  3. Django Settings:

    • Verify that you've set the correct value for the "DJANGO_SETTINGS_MODULE" environment variable in your WSGI file. It should point to your Django project's settings module, as you've done:

      os.environ['DJANGO_SETTINGS_MODULE'] = 'unitree.settings'
      
  4. Installed Apps:

    • Double-check that you have included the "api" app in your Django project's "INSTALLED_APPS" list in the "settings.py" file:

      INSTALLED_APPS = [
          ...
          'api',
          ...
      ]
      
  5. URLs Configuration:

    • Ensure that the "api.urls" module is correctly referenced in your "features/urls.py" file:

      path('api/', include('api.urls')),
      
  6. Virtual Environment:

    • Make sure you are using the correct virtual environment where you installed your project dependencies.
  7. Restart Server:

    • After making these changes, restart your web server to apply the configuration changes.

If you've verified all these points and are still encountering the error, please provide more details about your project's directory structure and any other relevant configuration files for further assistance.

英文:

I deployed my django project to pythonanywhere, but when I enter the page, this error is displayed in the logs:
Error running WSGI application
ModuleNotFoundError: No module named 'api.urls'

Structure:
/home/danialsk/unitree/       # Main project directory
    unitree/                   # Main project's Python package
        settings.py            # Django settings file
        urls.py                # Main project's URL configuration file
        ...
    api/                       # 'api' app directory
        urls.py                # URLs for the 'api' app
        ...
    features/
        urls.py                # URLs for the 'features' app
        ...
pip list:
(env) 13:42 ~/unitree/unitree (master)$ pip list
Package             Version
------------------- --------
api                 0.0.7
asgiref             3.6.0
blinker             1.6.2
certifi             2023.5.7
charset-normalizer  3.1.0
click               8.1.3
colorama            0.4.6
Django              4.2.1
django-ckeditor     6.5.1
django-embed-video  1.4.8
django-filter       23.2
django-js-asset     2.0.0
djangorestframework 3.14.0
Flask               2.3.2
gunicorn            20.1.0
idna                3.4
itsdangerous        2.1.2
Jinja2              3.1.2
Markdown            3.4.3
MarkupSafe          2.1.2
nose                1.3.7
Pillow              9.5.0
pip                 22.1.2
psycopg2            2.9.6
pytz                2023.3
requests            2.30.0
setuptools          62.6.0
sqlparse            0.4.4
tzdata              2023.3
urllib3             2.0.2
Werkzeug            2.3.4
wheel               0.37.1
whitenoise          6.4.0

WSGI file:

import os
import sys

# assuming your django settings file is at '/home/danialsk/mysite/mysite/settings.py'
# and your manage.py is is at '/home/danialsk/mysite/manage.py'
path = '/home/danialsk/unitree/unitree'

if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'unitree.settings'

# then:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Main urls:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('features.urls')),
]


urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_URL)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Feautures urls:

from django.urls import path, include
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('api/', include('api.urls')),
]

Settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'features.apps.FeaturesConfig',
    'api',
    'embed_video',
    'ckeditor',
    'ckeditor_uploader',
    'rest_framework',
]

It returns this error: Error running WSGI application
ModuleNotFoundError: No module named 'api.urls'

Full error line:

Error running WSGI application
2023-05-21 14:41:34,122: ModuleNotFoundError: No module named 'api.urls'
2023-05-21 14:41:34,122:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/core/handlers/wsgi.py", line 124, in __call__
2023-05-21 14:41:34,122:     response = self.get_response(request)
2023-05-21 14:41:34,122: 
2023-05-21 14:41:34,122:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/core/handlers/base.py", line 140, in get_response
2023-05-21 14:41:34,122:     response = self._middleware_chain(request)
2023-05-21 14:41:34,122: 
2023-05-21 14:41:34,122:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/core/handlers/exception.py", line 57, in inner
2023-05-21 14:41:34,123:     response = response_for_exception(request, exc)
2023-05-21 14:41:34,123: 
2023-05-21 14:41:34,123:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/core/handlers/exception.py", line 140, in response_for_exception
2023-05-21 14:41:34,123:     response = handle_uncaught_exception(
2023-05-21 14:41:34,123: 
2023-05-21 14:41:34,123:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/core/handlers/exception.py", line 181, in handle_uncaught_exception
2023-05-21 14:41:34,123:     return debug.technical_500_response(request, *exc_info)
2023-05-21 14:41:34,123: 
2023-05-21 14:41:34,123:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/views/debug.py", line 67, in technical_500_response
2023-05-21 14:41:34,123:     html = reporter.get_traceback_html()
2023-05-21 14:41:34,123: 
2023-05-21 14:41:34,124:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/views/debug.py", line 410, in get_traceback_html
2023-05-21 14:41:34,124:     c = Context(self.get_traceback_data(), use_l10n=False)
2023-05-21 14:41:34,124: 
2023-05-21 14:41:34,124:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/views/debug.py", line 393, in get_traceback_data
2023-05-21 14:41:34,124:     c["raising_view_name"] = get_caller(self.request)
2023-05-21 14:41:34,124: 
2023-05-21 14:41:34,124:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/views/debug.py", line 100, in get_caller
2023-05-21 14:41:34,124:     resolver_match = resolve(request.path)
2023-05-21 14:41:34,124: 
2023-05-21 14:41:34,124:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/urls/base.py", line 24, in resolve
2023-05-21 14:41:34,125:     return get_resolver(urlconf).resolve(path)
2023-05-21 14:41:34,125: 
2023-05-21 14:41:34,125:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/urls/resolvers.py", line 663, in resolve
2023-05-21 14:41:34,125:     for pattern in self.url_patterns:
2023-05-21 14:41:34,125: 
2023-05-21 14:41:34,125:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/utils/functional.py", line 57, in __get__
2023-05-21 14:41:34,125:     res = instance.__dict__[self.name] = self.func(instance)
2023-05-21 14:41:34,125: 
2023-05-21 14:41:34,125:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/urls/resolvers.py", line 715, in url_patterns
2023-05-21 14:41:34,125:     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
2023-05-21 14:41:34,126: 
2023-05-21 14:41:34,126:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/utils/functional.py", line 57, in __get__
2023-05-21 14:41:34,126:     res = instance.__dict__[self.name] = self.func(instance)
2023-05-21 14:41:34,126: 
2023-05-21 14:41:34,126:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/urls/resolvers.py", line 708, in urlconf_module
2023-05-21 14:41:34,126:     return import_module(self.urlconf_name)
2023-05-21 14:41:34,126: 
2023-05-21 14:41:34,126:   File "/home/danialsk/unitree/unitree/unitree/urls.py", line 9, in <module>
2023-05-21 14:41:34,126:     path('', include('features.urls')),
2023-05-21 14:41:34,126: 
2023-05-21 14:41:34,126:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/urls/conf.py", line 38, in include
2023-05-21 14:41:34,126:     urlconf_module = import_module(urlconf_module)
2023-05-21 14:41:34,126: 
2023-05-21 14:41:34,127:   File "/home/danialsk/unitree/unitree/features/urls.py", line 12, in <module>
2023-05-21 14:41:34,127:     path('api/', include('api.urls')),
2023-05-21 14:41:34,127: 
2023-05-21 14:41:34,127:   File "/home/danialsk/.virtualenvs/env/lib/python3.10/site-packages/django/urls/conf.py", line 38, in include
2023-05-21 14:41:34,127:     urlconf_module = import_module(urlconf_module)

How to fix that?

答案1

得分: 0

你需要将 path 设置为根目录(manage.py 所在的位置),所以请将你 wsgi 文件中的 path 从以下内容:

path = '/home/danialsk/unitree/unitree'

修改为:

path = '/home/danialsk/unitree'

另外,你的 unitree.urls 中存在重复的 urlpattern,请将其更新为如下内容:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('features.urls')),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_URL)
英文:

You've to set path to root directory (where manage.py lies)
so change your path in wsgi file from this

path = '/home/danialsk/unitree/unitree'

to this

path = '/home/danialsk/unitree'

and you've duplicate urlpattern in your unitree.urls
update it like this

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('features.urls')),
]


urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_URL)

huangapple
  • 本文由 发表于 2023年5月21日 22:58:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定