HTTP 500 错误 - 无法将目标 WSGI 脚本加载为 Python 模块

HTTP 500 error - Target WSGI script cannot be loaded as Python module

我在使用 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 

该应用程序运行良好。

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

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 配置中有错误。任何帮助或建议将不胜感激!

感谢 Jota 提供其中一种解决方案。

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

方案二: 如果您不想使用 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) 重启后,我的自定义项目运行成功了。

此致,

Amey Kelekar