【问题标题】:HTTP 500 error - Target WSGI script cannot be loaded as Python moduleHTTP 500 错误 - 目标 WSGI 脚本无法作为 Python 模块加载
【发布时间】:2018-06-23 01:43:43
【问题描述】:

我在使用 bitnami django 时遇到了上述错误。 根据文档完成所有设置: https://docs.bitnami.com/virtual-machine/components/django/#production python版本:Python 3.6.4 :: Anaconda, Inc. django 版本:2.0.2-3

wsgi.py:

import os,sys
sys.path.append('/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject')
os.environ.setdefault("PYTHON_EGG_CACHE", "/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/egg_cache")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MyProject.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

httpd-app.conf:

<IfDefine !IS_DJANGOSTACK_LOADED> 
Define IS_DJANGOSTACK_LOADED
WSGIDaemonProcess wsgi-djangostack   processes=2 threads=15    display-name=%{GROUP}
</IfDefine> 

<Directory "/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/MyProject">
    Options +MultiViews
    AllowOverride All
    <IfVersion < 2.3 >
        Order allow,deny
        Allow from all
    </IfVersion>
    <IfVersion >= 2.3>
        Require all granted
    </IfVersion>

    WSGIProcessGroup wsgi-djangostack

WSGIApplicationGroup %{GLOBAL}
Require all granted

</Directory>

Alias /MyProject/static "/Applications/djangostack-2.0.2-3/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/contrib/admin/static"
WSGIScriptAlias /MyProject '/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/MyProject/wsgi.py'

httpd-prefix.conf:

# Include file
Include "/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/conf/httpd-app.conf"

对应的 url 添加到 /Applications/djangostack-2.0.2-3/apache2/conf/bitnami/bitnami-apps-prefix.conf 文件

但是当我尝试通过浏览器访问应用程序时,日志中出现错误:

[Fri Jun 22 16:37:20.873873 2018] [wsgi:error] [pid 29099] [remote ::1:62098] mod_wsgi (pid=29099): Target WSGI script '/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/MyProject/wsgi.py' cannot be loaded as Python module.
[Fri Jun 22 16:37:20.874027 2018] [wsgi:error] [pid 29099] [remote ::1:62098] mod_wsgi (pid=29099): Exception occurred processing WSGI script '/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/MyProject/wsgi.py'.
[Fri Jun 22 16:37:20.875482 2018] [wsgi:error] [pid 29099] [remote ::1:62098] Traceback (most recent call last):
[Fri Jun 22 16:37:20.875554 2018] [wsgi:error] [pid 29099] [remote ::1:62098]   File "/Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/MyProject/wsgi.py", line 17, in <module>
[Fri Jun 22 16:37:20.875568 2018] [wsgi:error] [pid 29099] [remote ::1:62098]     application = get_wsgi_application()
[Fri Jun 22 16:37:20.875584 2018] [wsgi:error] [pid 29099] [remote ::1:62098]   File "/Applications/djangostack-2.0.2-3/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Jun 22 16:37:20.875594 2018] [wsgi:error] [pid 29099] [remote ::1:62098]     django.setup(set_prefix=False)
[Fri Jun 22 16:37:20.875608 2018] [wsgi:error] [pid 29099] [remote ::1:62098]   File "/Applications/djangostack-2.0.2-3/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/__init__.py", line 24, in setup
[Fri Jun 22 16:37:20.875618 2018] [wsgi:error] [pid 29099] [remote ::1:62098]     apps.populate(settings.INSTALLED_APPS)
[Fri Jun 22 16:37:20.875631 2018] [wsgi:error] [pid 29099] [remote ::1:62098]   File "/Applications/djangostack-2.0.2-3/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/apps/registry.py", line 89, in populate
[Fri Jun 22 16:37:20.875641 2018] [wsgi:error] [pid 29099] [remote ::1:62098]     app_config = AppConfig.create(entry)
[Fri Jun 22 16:37:20.875654 2018] [wsgi:error] [pid 29099] [remote ::1:62098]   File "/Applications/djangostack-2.0.2-3/apps/django/lib/python3.6/site-packages/Django-2.0.2-py3.6.egg/django/apps/config.py", line 90, in create
[Fri Jun 22 16:37:20.875664 2018] [wsgi:error] [pid 29099] [remote ::1:62098]     module = import_module(entry)
[Fri Jun 22 16:37:20.875677 2018] [wsgi:error] [pid 29099] [remote ::1:62098]   File "/Applications/djangostack-2.0.2-3/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
[Fri Jun 22 16:37:20.875687 2018] [wsgi:error] [pid 29099] [remote ::1:62098]     return _bootstrap._gcd_import(name[level:], package, level)
[Fri Jun 22 16:37:20.875700 2018] [wsgi:error] [pid 29099] [remote ::1:62098]   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[Fri Jun 22 16:37:20.875728 2018] [wsgi:error] [pid 29099] [remote ::1:62098]   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Fri Jun 22 16:37:20.875736 2018] [wsgi:error] [pid 29099] [remote ::1:62098]   File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[Fri Jun 22 16:37:20.875752 2018] [wsgi:error] [pid 29099] [remote ::1:62098] ModuleNotFoundError: No module named 'widget_tweaks'

我已经安装了 widget_tweaks 使用:

pip3 install django-widget-tweaks 

但是当我停止 Apache 并使用命令运行应用程序时

python3 manage.py runserver localhost:8080 

该应用运行良好。

我在 /Applications/djangostack-2.0.2-3/apps/django/django_projects/MyProject/MyProject 下的 urls.py 看起来像:

from django.contrib import admin
from django.urls import path, include,re_path

urlpatterns = [
    path('', include('APP.urls')),
    re_path(r'^admin/', admin.site.urls, name='admin'),
]

所以我假设 Apache 配置中存在错误。任何帮助或建议将不胜感激!

【问题讨论】:

  • 您是如何安装为 Anaconda Python 编译的 mod_wsgi 版本的? mod_wsgi 的系统包不能与 Anaconda Python 一起使用,因为 mod_wsgi 需要使用您要使用的特定 Python 安装/版本进行编译。
  • 当你看到这个错误信息时,你调用了哪个 url?
  • 我可以看看你的 urls.py
  • 嗨@Mohammad,我正在编辑问题以附上我的urls.py
  • 我调用的 URL 是 localhost:8080/MyProject

标签: django python-3.x apache mod-wsgi bitnami


【解决方案1】:

感谢 Jota 提供解决方案之一。

解决方案 1: 对于 Mac OS,当我执行 /Applications/djangostack-2.0.2-3/use_djangostack 时,我进入 Bitnami 控制台。 我只需要安装所有必要的模块,并且自定义项目使用 apache 成功运行。

解决方案 2: 如果您不想使用 bitnami 控制台,

1) 使用 pip 安装 mod_wsgi,最好安装到 Python 虚拟环境中。确保 pip 适用于您要使用的 Python 版本。

pip install mod_wsgi 

2) 通过运行显示要添加到 Apache 配置文件以加载此 mod_wsgi 的配置:

mod_wsgi-express module-config

3) 获取上述命令的输出以显示配置并添加到 Apache 配置。 (httpd.conf 文件)

4) 使用 ctlscript.sh 文件重新启动 apache。在我的情况下,命令看起来像:

/Applications/djangostack-2.0.2-3/ctlscript.sh restart apache

5) 重启后,我的自定义项目运行成功。

问候,

艾米·凯莱卡

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 2011-09-21
    • 1970-01-01
    • 2015-12-11
    • 2018-01-18
    • 2018-06-23
    • 1970-01-01
    • 2018-07-20
    相关资源
    最近更新 更多