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

时间:2018-06-23 01:43:43

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

使用bitnami django时出现上述错误。 所有设置均根据doc完成: 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"

相应的网址已添加到/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配置内存在错误。任何帮助或建议将不胜感激!

1 个答案:

答案 0 :(得分: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)使用上述命令的输出来显示config并添加到Apache配置中。 (httpd.conf文件)

4)使用ctlscript.sh文件重新启动apache。在我的情况下,命令如下所示:

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

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

此致

Amey Kelekar