简体   繁体   English

无法将目标 WSGI 脚本加载为 Python 模块

[英]Target WSGI script cannot be loaded as Python module

I am trying to deploy mod_wsgi with apache to run a django application but I am getting an error 500 internal server error The apache logs shows:我正在尝试使用 apache 部署 mod_wsgi 以运行 django 应用程序,但我收到错误 500 内部服务器错误 apache 日志显示:

[Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64] mod_wsgi (pid=16142): Exception occurred processing WSGI script '/home/user/bms/apache/django.wsgi'.
[Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64] Traceback (most recent call last):
[Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64]   File "/home/user/bms/apache/django.wsgi", line 13, in <module>
[Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64]     import django.core.handlers.wsgi
[Thu Jun 23 14:01:47 2011] [error] [client 152.78.95.64] ImportError: No module named django.core.handlers.wsgi

My apache virtual host is as follows:我的apache虚拟主机如下:

<VirtualHost *:80>

    DocumentRoot /home/user/bms

    <Directory /home/user/bms>
        Order allow,deny
        Allow from all
    </Directory>

WSGIDaemonProcess bms user=user group=user processes=2 threads=25 python-path=/usr/local/lib/python2.7/site-packages


    WSGIProcessGroup bms

    WSGIScriptAlias / /home/user/bms/apache/django.wsgi

</VirtualHost>

And the referenced wsgi file in my app directory with 0777 permissions:以及我的应用程序目录中具有 0777 权限的引用 wsgi 文件:

import os
import sys

path = '/home/user/bms'
if path not in sys.path:
    sys.path.append(path)

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

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

I heard that this may be because the apache user does not have the correct permissions.我听说这可能是因为 apache 用户没有正确的权限。 However I have no idea how to fix this.但是我不知道如何解决这个问题。 I also tried starting the deamon with the www-data user and this did not solve the issue.我还尝试用 www-data 用户启动守护进程,但这并没有解决问题。

EDIT :编辑

I solved this by copying the virtual hosts file into the default one and then disabling the old one with a2dissite.我通过将虚拟主机文件复制到默认文件然后使用 a2dissite 禁用旧文件来解决这个问题。 I have no idea how I can do it "properly" and set it so apache goes to the virtual host I want it to though.我不知道如何“正确”地做到这一点并将其设置为 apache 转到我想要的虚拟主机。

For me the problem was wsgi python version mismatch.对我来说,问题是 wsgi python 版本不匹配。 I was using python 3, so:我用的是 python 3,所以:

$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3

Warning from @alxs before you copy/paste these commands:在复制/粘贴这些命令之前来自@alxs 的警告:
If there are python 2 projects running on the server that use wsgi and apache, the above commands will effectively shut them down.如果在服务器上运行了 python 2个使用wsgi的项目和 apache ,以上命令将有效地关闭它们。

For me the issue was that the WSGI script wasn't executable.对我来说,问题是 WSGI 脚本不可执行。

sudo chmod a+x django.wsgi

or just要不就

sudo chmod u+x django.wsgi

so long as you have the correct owner只要你有正确的所有者

I had a similar problem with this error message in the logs:我在日志中遇到了与此错误消息类似的问题:

Target WSGI script '/home/web2py/wsgihandler.py' cannot be loaded as Python module.无法将目标 WSGI 脚本“/home/web2py/wsgihandler.py”加载为 Python 模块。

The solution was the deletion of an incorrect WSGIPythonHome directive (pointing to the application directory) from /etc/httpd/conf.d/wsgi.conf解决方案是从/etc/httpd/conf.d/wsgi.conf中删除不正确的 WSGIPythonHome 指令(指向应用程序目录)

I'm on RedHat using CentOS repositories.我在 RedHat 上使用 CentOS 存储库。

Recommend following Graham Dumpleton's installation/configuration instructions.建议遵循 Graham Dumpleton 的安装/配置说明。 Testing configuration against the helloworld application showed me that mod_wsgi was working and the configuration was at fault.针对 helloworld 应用程序的测试配置向我展示了mod_wsgi正在工作并且配置有问题。

However, the error message gave little clue as to what was wrong.但是,错误消息几乎没有给出错误的线索。

I had the same problem and at first I didn't realise I could scroll further down and see the actual error message.我遇到了同样的问题,起初我没有意识到我可以进一步向下滚动并查看实际的错误消息。 In my case, it was an import error:就我而言,这是一个导入错误:

ImportError: No module named bootstrap3

After installing it via pip (pip install django-bootstrap3), I restarted Apache and it worked.通过 pip (pip install django-bootstrap3) 安装后,我重新启动 Apache 并且它工作了。

Appending path in wsgi.py is the direction, but instead of appending django appending path sys.path.append("/path/to/virtual/environment/lib/pythonX.X/site-packages") fixed my case.wsgi.py中附加路径是方向,但不是附加django附加路径sys.path.append("/path/to/virtual/environment/lib/pythonX.X/site-packages")修复了我的情况。

This is for a django project using python2.7 on ubuntu 16.04.这是一个在 ubuntu 16.04 上使用 python2.7 的 django 项目。

I know this question is pretty old, but I wrestled with this for about eight hours just now.我知道这个问题已经很老了,但我刚才为此纠结了大约八个小时。 If you have a system with SELinux enabled and you've put your virtualenv in particular places, mod_wsgi won't be able to add your specified python-path to the site-packages.如果你有一个启用了 SELinux 的系统并且你已经将你的 virtualenv 放在特定的地方,mod_wsgi 将无法将你指定python-path添加到站点包。 It also won't raise any errors;它也不会引发任何错误; as it turns out the mechanism it uses to add the specified python-path to the site packages is with the Python site module, specifically site.adduserdir() .事实证明,它用于将指定的python-path添加到站点包的机制是使用 Python site模块,特别是site.adduserdir() This method doesn't raise any errors if the directory is missing or can't be accessed, so mod_wsgi also doesn't raise any errors.如果目录丢失或无法访问,此方法不会引发任何错误,因此 mod_wsgi 也不会引发任何错误。

Anyway, try turning off SELinux with无论如何,尝试关闭 SELinux

sudo setenforce 0

or by making sure that the process you're running Apache as has the appropriate ACLs with SELinux to access the directory the virtualenv is in.或者通过确保您正在运行的进程 Apache 具有适当的 ACL 和 SELinux 来访问 virtualenv 所在的目录。

In my own case on windows in xampp, I was incorrectly loading the application path in the wsgi.py file like so:在我自己的案例中,xampp 中的 windows,我错误地加载了 wsgi.py 文件中的应用程序路径,如下所示:

Incorrect :不正确

import sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "advisory_portal.settings")

application = get_wsgi_application()
sys.path.append('C:/xampp/htdocs/advisory_portal/advisory_portal')
sys.path.append('C:/xampp/htdocs/advisory_portal')

Instead of:代替:

Correct:正确的:

import sys

sys.path.append('C:/xampp/htdocs/advisory_portal/advisory_portal')
sys.path.append('C:/xampp/htdocs/advisory_portal')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "advisory_portal.settings")

application = get_wsgi_application()

Don't forget to import sys python package不要忘记导入 sys python package

I was getting this error.我收到了这个错误。 I'm using python 3 in a virtual environment found this in my apache logs我在虚拟环境中使用 python 3 在我的 apache 日志中找到了这个

[Fri Dec 21 08:01:43.471561 2018] [mpm_prefork:notice] [pid 21786] AH00163: Apache/2.4.6 (Red Hat Enterprise Linux) mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations [Fri Dec 21 08:01:43.471561 2018] [mpm_prefork:notice] [pid 21786] AH00163: Apache/2.4.6 (Red Hat Enterprise Linux) mod_wsgi/3.4 Python/2.7.5 配置——恢复正常操作

I had installed wsgi using yum -y install mod_wsgi .我使用yum -y install mod_wsgi安装了 wsgi。 This had installed mod_wsgi compiled for python 2. So I uninstalled it这已经安装了为 python 编译的 mod_wsgi 2. 所以我卸载了它

yum remove mod_wsgi

and installed mod_wsgi compiled for python 3 using并安装了为 python 3 编译的 mod_wsgi 使用

yum install python35u-mod_wsgi

It worked after that在那之后它起作用了

I had the same error我有同样的错误

Target WSGI script cannot be loaded as Python module无法将目标 WSGI 脚本加载为 Python 模块

This was because the python path was not added in the apache.conf这是因为apache.conf中没有添加python路径

Depending on the Apache and Ubuntu version you need to update the apache.conf or httpd.conf file with the pythonWSGIPath根据 Apache 和 Ubuntu 版本,您需要使用 pythonWSGIPath 更新 apache.conf 或 httpd.conf 文件

In my case I need to edit the apache.conf就我而言,我需要编辑 apache.conf

sudo nano /etc/apache2/apache2.conf

Then add the following line at the end (Change the my_project to the project your project name)然后在末尾添加以下行(将 my_project 更改为您的项目名称的项目)

WSGIPythonPath /var/www/my_project

And everything works with charm一切都充满魅力

Did you try it without the WSGIDaemonProcess option?您是否在没有 WSGIDaemonProcess 选项的情况下尝试过?

I had no trouble setting up mod_wsgi at home, but did it without the daemon option.我在家里设置 mod_wsgi 时没有遇到任何问题,但没有使用守护进程选项。 You mentioned solving by moving around virtual hosts files and I note this caveat in the docs for WSGIDaemonProcess:您提到通过移动虚拟主机文件来解决问题,我在 WSGIDaemonProcess 的文档中注意到了这个警告:

Also note that the name of the daemon process group must be unique for the whole server.另请注意,守护进程组的名称对于整个服务器必须是唯一的。 That is, it is not possible to use the same daemon process group name in different virtual hosts.即不能在不同的虚拟主机中使用相同的守护进程组名。

Don't know if that's coincidence.不知道是不是巧合。

As this question became sort of a pool for collecting solutions for problems that result in the error giving this question its title, I'd like to add this one, too.由于这个问题变成了一个池子,用于收集导致错误的问题的解决方案,因此我也想添加这个问题。

In my case, I want to run OpenStack Keystone (Ocata) using Apache and WSGI on Ubuntu 16.04.2.就我而言,我想在 Ubuntu 16.04.2 上使用 Apache 和 WSGI 运行 OpenStack Keystone (Ocata)。 The processes start but as soon as I query keystone I get进程开始了,但是一旦我查询 keystone,我就得到了

mod_wsgi (pid=20103): Target WSGI script '/opt/openstack/bin/keystone-wsgi-public' cannot be loaded as Python module.

I had two vhosts, one had我有两个虚拟主机,一个有

WSGIDaemonProcess keystone-public ...
WSGIProcessGroup keystone-public ...

while the other had而另一个有

WSGIDaemonProcess keystone-admin ...
WSGIProcessGroup keystone-admin ...

I solved the problem by renaming them.我通过重命名解决了这个问题。 The vhost entries now read: vhost 条目现在显示为:

WSGIDaemonProcess kst-pub ...
WSGIProcessGroup kst-pub ...

and

WSGIDaemonProcess kst-adm ...
WSGIProcessGroup kst-adm ...

I didn't investigate any further.我没有进一步调查。 Solved as works for me.解决了对我有用。

If you install your project's Python dependencies inside a virtualenv, you'll need to add the path to this virtualenv's directory to your Python path as well.如果在 virtualenv 中安装项目的 Python 依赖项,则还需要将此 virtualenv 目录的路径添加到 Python 路径中。 To do this, add an additional path to your WSGIPythonPath directive, with multiple paths separated by a colon (:) if using a UNIX-like system, or a semicolon (;) if using Windows为此,请向您的 WSGIPythonPath 指令添加一个附加路径,如果使用类 UNIX 系统,则多个路径由冒号 (:) 分隔,如果使用 Windows,则由分号 (;) 分隔

Adding on to the list this is how I got it working.添加到列表中,这就是我让它工作的方式。

I was trying to install CKAN 2.7.2 on CentOS 7 from source and kept coming up against this error.我试图从源代码在 CentOS 7 上安装 CKAN 2.7.2,但一直遇到此错误。 For me it was because SELinux was enabled.对我来说,这是因为启用了 SELinux。 I didn't need to disable it.我不需要禁用它。 Instead, after reading https://www.endpoint.com/blog/2010/10/13/selinux-httpd-modwsgi-26-rhel-centos-5 , I found that turning on httpd_can.network_connect fixed it:相反,在阅读https://www.endpoint.com/blog/2010/10/13/selinux-httpd-modwsgi-26-rhel-centos-5后,我发现打开 httpd_can.network_connect 修复了它:

setsebool -P httpd_can_network_connect on

From that page:从那个页面:

httpd_can.network_connect - Allows httpd to make.network connections, including the local ones you'll be making to a database httpd_can.network_connect - 允许 httpd 建立网络连接,包括您将建立的本地数据库连接

I had similar issue eg apache log error "wsgi.py cannot be loaded as Python module."我有类似的问题,例如 apache 日志错误“wsgi.py 无法加载为 Python 模块。”

It turned out I had to stop and then start apache instead of just restarting it.原来我不得不停止然后启动 apache 而不是重新启动它。

I had the same problem and it got solved using我遇到了同样的问题,并使用它解决了

sudo easy_install cx_Oracle

but remember to unistall cx_oracle before installing it using easy_install .但请记住在使用easy_install安装之前先卸载 cx_oracle。

Command to uninstall: pip uninstall cx_oracle卸载命令: pip uninstall cx_oracle

The solution that finally worked for me, after trying many of these options unsuccessfully was simple, but elusive because I struggled to figure out what actual paths to use.在尝试了许多这些选项失败后,最终对我有用的解决方案很简单,但难以捉摸,因为我努力弄清楚要使用的实际路径。

I created a mezzanine project, which is based on django, with the following commands.我使用以下命令创建了一个基于 django 的夹层项目。 I list them here to make the paths explicit.我在这里列出它们以使路径明确。

/var/www/mysite$ python3 -m venv ./venv
/var/www/mysite$ source ./venv/bin/activate
(venv) /var/www/mysite$ mezzanine-project mysite
(venv) /var/www/mysite$ cd mysite
(venv) /var/www/mysite/mysite$

Now, the path to the wsgi.py file is:现在,wsgi.py 文件的路径是:

/var/www/mysite/mysite/mysite/wsgi.py

The directives that worked for this installation within my /etc/apache2/sites-available/mysite.conf file follow:在我的/etc/apache2/sites-available/mysite.conf文件中适用于此安装的指令如下:

...<VirtualHost...>
    ...
    WSGIDaemonProcess mysite python-home=/var/www/mysite/venv python-path=/var/www/mysite/mysite
    WSGIProcessGroup mysite
    WSGIScriptAlias / /var/www/mysite/mysite/mysite/wsgi.py process-group=accounting
    <Directory /var/www/mysite/mysite/mysite>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    ...
</VirtualHost>...

I tried numerous versions of python-home and python-path and got the OP's error repeatedly.我尝试了多个版本的 python-home 和 python-path 并反复收到 OP 的错误。 Using the correct paths here should also accomplish the same things as @Dev's answer without having to add paths in the wsgi.py file (supplied by mezzanine , and no editing necessary in my case).在这里使用正确的路径也应该完成与@Dev的回答相同的事情,而不必在 wsgi.py 文件中添加路径(由mezzanine提供,在我的情况下不需要编辑)。

Sometimes when I get stuck on this I hard code a path to project in the wsgi file like:有时当我遇到这个问题时,我会在 wsgi 文件中硬编码一个项目路径,例如:

import os
import sys


sys.path.append("/var/www/html/myproject")
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

application = get_wsgi_application()

I recommend trying to downgrade DJANGO to version 2.1.1 .我建议尝试将 DJANGO 降级到版本2.1.1

Making sure my project's venv folder resides inside my project's root directory solved this problem for me.确保我的项目的venv文件夹位于我项目的根目录中为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 目标 WSGI 脚本不能作为 python 模块加载 - Target WSGI script cannot be loaded as a python module mod_wsgi目标WSGI脚本无法作为Python模块加载 - mod_wsgi Target WSGI script cannot be loaded as Python module 目标WSGI脚本wsgi.py&#39;无法作为Python模块加载 - Target WSGI script wsgi.py' cannot be loaded as Python module 无法将目标WSGI脚本作为Python模块+ ImportError加载:未命名模块 - Target WSGI script cannot be loaded as Python module + ImportError: No module named 目标 WSGI 脚本无法加载为 Python 模块 + 没有名为 Django 的模块 - Target WSGI script cannot be loaded as Python module + no module named Django 目标WSGI脚本无法作为Django App中的Python模块加载 - Target WSGI Script Cannot Be Loaded as Python Module in Django App 目标 WSGI 脚本无法加载为 Python 模块。Flask.Apache - Target WSGI script cannot be loaded as Python module.Flask.Apache 无法将目标WSGI脚本作为Python模块Raspberry Pi加载 - Target WSGI script cannot be loaded as Python module Raspberry Pi apache2 和 mod wsgi:目标 WSGI 脚本无法作为 Python 模块加载 - apache2 and mod wsgi : Target WSGI script cannot be loaded as Python module 目标WSGI脚本&#39;/home/ubuntu/Django/bot/wsgi.py&#39;无法作为Python模块加载 - Target WSGI script '/home/ubuntu/Django/bot/wsgi.py' cannot be loaded as Python module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM