ImportError:没有名为cv2的模块 - WSGI + python + apache

时间:2016-08-16 18:32:17

标签: python apache opencv amazon-web-services amazon-ec2

我试图通过WSGI在我的apache Amazon EC2服务器上运行python应用程序,并且我一直收到此错误:

[Tue Aug 16 18:22:57 2016] [error] [client 72.219.147.5] mod_wsgi (pid=28751): Target WSGI script '/var/www/html/lumos/wsgi.py' cannot be loaded as Python module.
[Tue Aug 16 18:22:57 2016] [error] [client 72.219.147.5] mod_wsgi (pid=28751): Exception occurred processing WSGI script '/var/www/html/lumos/wsgi.py'.
[Tue Aug 16 18:22:57 2016] [error] [client 72.219.147.5] Traceback (most recent call last):
[Tue Aug 16 18:22:57 2016] [error] [client 72.219.147.5]   File "/var/www/html/lumos/wsgi.py", line 11, in <module>
[Tue Aug 16 18:22:57 2016] [error] [client 72.219.147.5]     import app
[Tue Aug 16 18:22:57 2016] [error] [client 72.219.147.5]   File "/var/www/html/lumos/app.py", line 2, in <module>
[Tue Aug 16 18:22:57 2016] [error] [client 72.219.147.5]     import main
[Tue Aug 16 18:22:57 2016] [error] [client 72.219.147.5]   File "/var/www/html/lumos/main.py", line 1, in <module>
[Tue Aug 16 18:22:57 2016] [error] [client 72.219.147.5]     import mod_one
[Tue Aug 16 18:22:57 2016] [error] [client 72.219.147.5]   File "/var/www/html/lumos/mod_one.py", line 1, in <module>
[Tue Aug 16 18:22:57 2016] [error] [client 72.219.147.5]     import cv2
[Tue Aug 16 18:22:57 2016] [error] [client 72.219.147.5] ImportError: No module named cv2

这是cv2.so文件所在的位置(sudo find / -name "cv2.so"):

/var/www/html/lumos/opencv/build/lib/cv2.so
/usr/local/lib/python2.7/dist-packages/cv2.so

我已将WSGI Python Path设置为该文件所在的位置:

WSGIPythonPath /usr/local/lib/python2.7/site-packages/:/usr/local/lib/python2.7/dist-packages/

我知道opencv已正确安装,因为当我执行以下操作时,没有错误:

$ python
>>>import cv2 #no import error
>>> 

当我安装mod_wsgi时,使用了它:

mod_wsgi-python26-3.2-6.11.amzn1.x86_64

这是我的wsgi.py文件

import os, sys

sys.path.insert(0, "/var/www/html/lumos")

import bottle
import app

application = bottle.default_app() #using bottle.py web-framework

这是我的httpd.conf:

WSGISocketPrefix /var/run/wsgi
WSGIPythonPath /usr/local/lib/python2.7/site-packages/:/usr/local/lib/python2.7/dist-packages/

<VirtualHost *>
ServerName lumos.website.me
DocumentRoot /var/www/html/lumos

WSGIDaemonProcess lumos threads=5
WSGIScriptAlias / /var/www/html/lumos/app.wsgi
        <Directory "/var/www/html/lumos">
                WSGIProcessGroup lumos
                WSGIApplicationGroup %{GLOBAL}
                Order deny,allow
                Allow from all
        </Directory>
</VirtualHost>

当我运行python -V时,我得到Python 2.7.10.

如何让mod_wsgi与opencv一起使用?任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

好的,事实证明,根据the docs,你在使用守护进程模式时不能使用WSGIPythonPath。

所以我指定的python路径甚至没有做任何事情。为了解决这个问题,我使用了&#39; python-path&#39;相反,WSGIDaemonProcess directive的选项。

在我的httpd.conf文件中,我删除了这个:

WSGIPythonPath /usr/local/lib/python2.7/site-packages/:/usr/local/lib/python2.7/dist-packages/

并改变了这个:

WSGIDaemonProcess lumos threads=5

对此:

WSGIDaemonProcess lumos threads=5 python-path=/usr/local/lib/python2.7/site-packages/:/usr/local/lib/python2.7/dist-packages/

所以我的最终httpd.conf看起来像这样:

<VirtualHost *>
ServerName lumos.website.me
DocumentRoot /var/www/html/lumos

WSGIDaemonProcess lumos threads=5 python-path=/usr/local/lib/python2.7/site-packages/:/usr/local/lib/python2.7/dist-packages/
WSGIScriptAlias / /var/www/html/lumos/wsgi.py
        <Directory "/var/www/html/lumos">
                WSGIProcessGroup lumos
                WSGIApplicationGroup %{GLOBAL}
                Order deny,allow
                Allow from all
        </Directory>
</VirtualHost>

现在cv2正常运作。

答案 1 :(得分:0)

您的设置已损坏,因为mod_wsgi是针对Python 2.6编译的,而不是专门针对您要使用的Python 2.7安装。您不应该将Python 2.7安装中的site-packagesdict-packages强制插入到Python 2.6环境的模块搜索路径中。首先,你仍然运行错误的Python版本,其次,这些目录中的任何扩展模块都可能会失败并可能导致进程崩溃。

您必须从系统软件包中卸载正在使用的mod_wsgi,并安装为Python 2.7编译的版本。因为您使用的是非标准的Python安装,所以您可能需要从源代码构建mod_wsgi。