简体   繁体   中英

ImportError: No module named cv2 — WSGI + python + apache

I'm trying to run a python app on my apache Amazon EC2 server through WSGI, and I keep getting this error:

[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

This is where the cv2.so file is located ( sudo find / -name "cv2.so" ):

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

And I have set the WSGI Python Path to be where that file is located:

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

I know opencv is installed correctly because when I do the following, there's no error:

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

When I installed mod_wsgi, this was used:

mod_wsgi-python26-3.2-6.11.amzn1.x86_64

Here is my wsgi.py file

import os, sys

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

import bottle
import app

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

Here is my 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>

When I run python -V , I get Python 2.7.10.

How can I make mod_wsgi work with opencv? Any help is appreciated.

Ok so it turns out, that according to the docs , you cannot use WSGIPythonPath when using daemon mode.

So the python path I had specified wasn't even doing anything. To fix, I used the 'python-path' option to the WSGIDaemonProcess directive instead.

In my httpd.conf file, I deleted this:

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

And changed this:

WSGIDaemonProcess lumos threads=5

To this:

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

So my final httpd.conf looks like this:

<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>

And now cv2 works.

Your setup is broken because mod_wsgi is compiled for Python 2.6 and not specifically for the Python 2.7 installation you want to use. You should not force the site-packages and dict-packages from your Python 2.7 installation into the module search path for what is a Python 2.6 environment. First up you are still running the wrong Python version and second, any extension modules in those directories will likely fail and possibly crash the processes.

You must uninstall the mod_wsgi you are using from system packages and install a version compiled for Python 2.7. Because you are using a non standard Python installation you likely will need to build mod_wsgi from source code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM