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

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

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:

      1. 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:

      1. 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:

      1. INSTALLED_APPS = [
      2. ...
      3. 'api',
      4. ...
      5. ]
  5. URLs Configuration:

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

      1. 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'

  1. Structure:
  2. /home/danialsk/unitree/ # Main project directory
  3. unitree/ # Main project's Python package
  4. settings.py # Django settings file
  5. urls.py # Main project's URL configuration file
  6. ...
  7. api/ # 'api' app directory
  8. urls.py # URLs for the 'api' app
  9. ...
  10. features/
  11. urls.py # URLs for the 'features' app
  12. ...
  1. pip list:
  2. (env) 13:42 ~/unitree/unitree (master)$ pip list
  3. Package Version
  4. ------------------- --------
  5. api 0.0.7
  6. asgiref 3.6.0
  7. blinker 1.6.2
  8. certifi 2023.5.7
  9. charset-normalizer 3.1.0
  10. click 8.1.3
  11. colorama 0.4.6
  12. Django 4.2.1
  13. django-ckeditor 6.5.1
  14. django-embed-video 1.4.8
  15. django-filter 23.2
  16. django-js-asset 2.0.0
  17. djangorestframework 3.14.0
  18. Flask 2.3.2
  19. gunicorn 20.1.0
  20. idna 3.4
  21. itsdangerous 2.1.2
  22. Jinja2 3.1.2
  23. Markdown 3.4.3
  24. MarkupSafe 2.1.2
  25. nose 1.3.7
  26. Pillow 9.5.0
  27. pip 22.1.2
  28. psycopg2 2.9.6
  29. pytz 2023.3
  30. requests 2.30.0
  31. setuptools 62.6.0
  32. sqlparse 0.4.4
  33. tzdata 2023.3
  34. urllib3 2.0.2
  35. Werkzeug 2.3.4
  36. wheel 0.37.1
  37. whitenoise 6.4.0

WSGI file:

  1. import os
  2. import sys
  3. # assuming your django settings file is at '/home/danialsk/mysite/mysite/settings.py'
  4. # and your manage.py is is at '/home/danialsk/mysite/manage.py'
  5. path = '/home/danialsk/unitree/unitree'
  6. if path not in sys.path:
  7. sys.path.append(path)
  8. os.environ['DJANGO_SETTINGS_MODULE'] = 'unitree.settings'
  9. # then:
  10. from django.core.wsgi import get_wsgi_application
  11. application = get_wsgi_application()

Main urls:

  1. from django.contrib import admin
  2. from django.urls import path, include
  3. from django.conf import settings
  4. from django.conf.urls.static import static
  5. urlpatterns = [
  6. path('admin/', admin.site.urls),
  7. path('', include('features.urls')),
  8. ]
  9. urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  10. urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_URL)
  11. urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Feautures urls:

  1. from django.urls import path, include
  2. from . import views
  3. urlpatterns = [
  4. path('', views.home, name='home'),
  5. path('api/', include('api.urls')),
  6. ]

Settings.py:

  1. INSTALLED_APPS = [
  2. 'django.contrib.admin',
  3. 'django.contrib.auth',
  4. 'django.contrib.contenttypes',
  5. 'django.contrib.sessions',
  6. 'django.contrib.messages',
  7. 'django.contrib.staticfiles',
  8. 'features.apps.FeaturesConfig',
  9. 'api',
  10. 'embed_video',
  11. 'ckeditor',
  12. 'ckeditor_uploader',
  13. 'rest_framework',
  14. ]

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

Full error line:

  1. Error running WSGI application
  2. 2023-05-21 14:41:34,122: ModuleNotFoundError: No module named 'api.urls'
  3. 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__
  4. 2023-05-21 14:41:34,122: response = self.get_response(request)
  5. 2023-05-21 14:41:34,122:
  6. 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
  7. 2023-05-21 14:41:34,122: response = self._middleware_chain(request)
  8. 2023-05-21 14:41:34,122:
  9. 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
  10. 2023-05-21 14:41:34,123: response = response_for_exception(request, exc)
  11. 2023-05-21 14:41:34,123:
  12. 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
  13. 2023-05-21 14:41:34,123: response = handle_uncaught_exception(
  14. 2023-05-21 14:41:34,123:
  15. 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
  16. 2023-05-21 14:41:34,123: return debug.technical_500_response(request, *exc_info)
  17. 2023-05-21 14:41:34,123:
  18. 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
  19. 2023-05-21 14:41:34,123: html = reporter.get_traceback_html()
  20. 2023-05-21 14:41:34,123:
  21. 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
  22. 2023-05-21 14:41:34,124: c = Context(self.get_traceback_data(), use_l10n=False)
  23. 2023-05-21 14:41:34,124:
  24. 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
  25. 2023-05-21 14:41:34,124: c["raising_view_name"] = get_caller(self.request)
  26. 2023-05-21 14:41:34,124:
  27. 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
  28. 2023-05-21 14:41:34,124: resolver_match = resolve(request.path)
  29. 2023-05-21 14:41:34,124:
  30. 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
  31. 2023-05-21 14:41:34,125: return get_resolver(urlconf).resolve(path)
  32. 2023-05-21 14:41:34,125:
  33. 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
  34. 2023-05-21 14:41:34,125: for pattern in self.url_patterns:
  35. 2023-05-21 14:41:34,125:
  36. 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__
  37. 2023-05-21 14:41:34,125: res = instance.__dict__[self.name] = self.func(instance)
  38. 2023-05-21 14:41:34,125:
  39. 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
  40. 2023-05-21 14:41:34,125: patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  41. 2023-05-21 14:41:34,126:
  42. 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__
  43. 2023-05-21 14:41:34,126: res = instance.__dict__[self.name] = self.func(instance)
  44. 2023-05-21 14:41:34,126:
  45. 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
  46. 2023-05-21 14:41:34,126: return import_module(self.urlconf_name)
  47. 2023-05-21 14:41:34,126:
  48. 2023-05-21 14:41:34,126: File "/home/danialsk/unitree/unitree/unitree/urls.py", line 9, in <module>
  49. 2023-05-21 14:41:34,126: path('', include('features.urls')),
  50. 2023-05-21 14:41:34,126:
  51. 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
  52. 2023-05-21 14:41:34,126: urlconf_module = import_module(urlconf_module)
  53. 2023-05-21 14:41:34,126:
  54. 2023-05-21 14:41:34,127: File "/home/danialsk/unitree/unitree/features/urls.py", line 12, in <module>
  55. 2023-05-21 14:41:34,127: path('api/', include('api.urls')),
  56. 2023-05-21 14:41:34,127:
  57. 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
  58. 2023-05-21 14:41:34,127: urlconf_module = import_module(urlconf_module)

How to fix that?

答案1

得分: 0

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

  1. path = '/home/danialsk/unitree/unitree'

修改为:

  1. path = '/home/danialsk/unitree'

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

  1. from django.contrib import admin
  2. from django.urls import path, include
  3. from django.conf import settings
  4. from django.conf.urls.static import static
  5. urlpatterns = [
  6. path('admin/', admin.site.urls),
  7. path('', include('features.urls')),
  8. ]
  9. urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  10. 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

  1. path = '/home/danialsk/unitree/unitree'

to this

  1. path = '/home/danialsk/unitree'

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

  1. from django.contrib import admin
  2. from django.urls import path, include
  3. from django.conf import settings
  4. from django.conf.urls.static import static
  5. urlpatterns = [
  6. path('admin/', admin.site.urls),
  7. path('', include('features.urls')),
  8. ]
  9. urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  10. 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:

确定