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

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.

Related

how to solve 500 internal server error mod_wsgi apache "importerror: No Module named 'django'

I have been trying to solve this error but I am not been able to solve, totally frustrated because I have tried every single answer on these related question , any help would be appreciated.
My configuration of VPS server
Ubuntu 18.04
django 2.2.5
apache 2.4.49
python 3.6(virtualenv)
libapache2-mod-wsgi-py3
My folder structure is:
/var/www/dataforze/prediction_project/venv (virtualenv folder)
bin
include
lib
/var/www/dataforze/prediction_project
|- manage.py
static
venv
prediction_project
|__init__.py
|settings.py
|urls.py
|wsgi.py
000-default.conf file code
<VirtualHost *:80>
<Directory /var/www/dataforze/prediction_project/prediction_project>
Require all granted
</Directory>
<Directory /var/www/dataforze/prediction_project/prediction_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess prediction_project python-path=/var/www/dataforze/prediction_project python-home=/var/www/dataforze/prediction_project/venv
WSGIProcessGroup prediction_project
WSGIScriptAlias / /var/www/dataforze/prediction_project/prediction_project/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/mysite_error.log
LogLevel warn
</VirtualHost>
my wsgi.py file code
import os
from django.core.wsgi import get_wsgi_application
sys.path.append('/var/www/dataforze/prediction_project/prediction_project')
# add the virtualenv site-packages path to the sys.path
sys.path.append('/var/www/dataforze/prediction_project/venv/lib/site-packages')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'prediction_project.settings')
application = get_wsgi_application()
My server logs
[Wed Mar 18 17:29:01.003624 2020] [wsgi:error] [pid 82169:tid 140431942575872] mod_wsgi (pid=82169): Target WSGI script '/var/www/dataforze/prediction_project/prediction_project/wsgi.py' cannot be loaded as Python module.
[Wed Mar 18 17:29:01.003722 2020] [wsgi:error] [pid 82169:tid 140431942575872] mod_wsgi (pid=82169): Exception occurred processing WSGI script '/var/www/dataforze/prediction_project/prediction_project/wsgi.py'.
[Wed Mar 18 17:29:01.003823 2020] [wsgi:error] [pid 82169:tid 140431942575872] Traceback (most recent call last):
[Wed Mar 18 17:29:01.003844 2020] [wsgi:error] [pid 82169:tid 140431942575872] File "/var/www/dataforze/prediction_project/prediction_project/wsgi.py", line 12, in <module>
[Wed Mar 18 17:29:01.003849 2020] [wsgi:error] [pid 82169:tid 140431942575872] from django.core.wsgi import get_wsgi_application
[Wed Mar 18 17:29:01.003864 2020] [wsgi:error] [pid 82169:tid 140431942575872] ModuleNotFoundError: No module named 'django'

Running Flask application from virtual environment

I have a simple flask app that i want to be accessible from internet.
OS is debian7, and unfortunately it can't be changed.
What I did:
- downloaded and installed Python3.4 into /usr/local/opt/python3.4.3
- installed wsgi lib for py3
- installed virtualenv and created new env in my flask project folder
- installed flask together with some other packages that I need within this env
- created VirtualHost as follows:
cat /etc/apache2/sites-available/Monitor
Listen 8080
<VirtualHost *:8080>
ServerName {ip address}
ServerAdmin {my email}
WSGIScriptAlias / /var/www/Monitor/monitor.wsgi
<Directory /var/www/Monitor/Monitor/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/Monitor/Monitor/static
<Directory /var/www/Monitor/Monitor/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
now when i try to access the app from the internet I see these errors in error.log
[Sat Nov 14 22:16:04 2015] [error] /usr/local/bin/python3
[Sat Nov 14 22:16:04 2015] [error] [client ip] mod_wsgi (pid=13450): Target WSGI script '/var/www/Monitor/monitor.wsgi' cannot be loaded as Python module.
[Sat Nov 14 22:16:04 2015] [error] [client ip] mod_wsgi (pid=13450): Exception occurred processing WSGI script '/var/www/Monitor/monitor.wsgi'.
[Sat Nov 14 22:16:04 2015] [error] [client ip] Traceback (most recent call last):
[Sat Nov 14 22:16:04 2015] [error] [client ip] File "/var/www/Monitor/monitor.wsgi", line 21, in <module>
[Sat Nov 14 22:16:04 2015] [error] [client ip] from Monitor import app as application
[Sat Nov 14 22:16:04 2015] [error] [client ip] File "/var/www/Monitor/Monitor/__init__.py", line 4, in <module>
[Sat Nov 14 22:16:04 2015] [error] [client ip] from flask import Flask, render_template, url_for
[Sat Nov 14 22:16:04 2015] [error] [client ip] File "/var/www/Monitor/Monitor/venv/lib/python3.4/site-packages/flask/__init__.py", line 17, in <module>
[Sat Nov 14 22:16:04 2015] [error] [client ip] from werkzeug.exceptions import abort
[Sat Nov 14 22:16:04 2015] [error] [client ip] File "/var/www/Monitor/Monitor/venv/lib/python3.4/site-packages/werkzeug/__init__.py", line 152, in <module>
[Sat Nov 14 22:16:04 2015] [error] [client ip] __import__('werkzeug.exceptions')
[Sat Nov 14 22:16:04 2015] [error] [client ip] File "/var/www/Monitor/Monitor/venv/lib/python3.4/site-packages/werkzeug/exceptions.py", line 113
[Sat Nov 14 22:16:04 2015] [error] [client ip] return u'<p>%s</p>' % escape(self.description)
[Sat Nov 14 22:16:04 2015] [error] [client ip] ^
[Sat Nov 14 22:16:04 2015] [error] [client ip] SyntaxError: invalid syntax
From what I could find in the internet, such errors appear when trying to start flask app with python version <= 3.3, which is not my case I think (how do i check?)
here is my wsgi that starts the app
#!/usr/bin/env python3
import os
import sys
import logging
logging.basicConfig(stream=sys.stderr)
PROJECT_DIR = '/var/www/Monitor/'
sys.path.insert(0, PROJECT_DIR)
def execfile(filename):
globals = dict(__file__ = filename)
exec(open(filename).read(), globals)
activate_this = '/var/www/Monitor/Monitor/venv/bin/activate_this.py'
execfile(activate_this)
from Monitor import app as application
Any help would be very much appreciated. Any additional information will be supplied if necessary.
Default Python3 on Debian 7 is 3.2.3, libapache2-mod-wsgi is in version 3.3.
You Apache run Your app with default Python because mod-wsgi has to be compiled against one and only one python version (in this case python 3.3).
mod-wsgi 3.3
If You want to run only one app (or many apps) with Python 3.4, try to recompile mod-wsgi with python 3.4
mod-wsgi 4.1.x or newer
This newer version of mod_wsgi provides a way of installing mod_wsgi against multiple Python versions and running up an Apache instance for each using a provided script. The script takes over all the setup of the Apache configuration so you do not need to worry about it.
https://pypi.python.org/pypi/mod_wsgi
old answer
To change Python version, set in Apache conf:
WSGIPythonHome /usr/local/opt/python3.4.3/
WSGIPythonPath /var/www/Monitor:/var/www/Monitor/Monitor/venv/lib/python3.4/site-packages:/usr/local/opt/python3.4.3/lib/site-packages
for details read https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIPythonHome

Django inside virtualenv keeps using wrong Python

I cannot, for the life of me, find a simple way to configure a (Linode Debian 6) server correctly with Django 1.8, Python 2.7.3, and virtualenv. Here's the steps I took:
mkvirtualenv --python=/usr/bin/python2.7 curator
This creates a VirtualEnv for me under /srv/ve/.virtualenvs/curator
Now I run:
pip install django
And Django 1.8 is successfully installed. Then, I switch to /srv/www/curator and run:
django-admin startproject cmscore
I then follow the Django Apache mod_wsgi instructions and end up with an Apache conf file under my sites-available directory looking like this:
<VirtualHost *:80>
ServerName cms.example.com
ServerAlias cms.example.com cms.example2.com
ServerAdmin errors#example.com
DocumentRoot /srv/www/curator/public_html
WSGIDaemonProcess cms.example.com python-path=/srv/ve/.virtualenvs/curator/bin/python:/srv/ve/.virtualenvs/curator/lib/python2.7/site-packages
WSGIProcessGroup cms.example.com
WSGIScriptAlias / /srv/www/curator/cmscore/cmscore/wsgi.py
<Directory /srv/www/curator/cmscore/cmscore>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
Alias /robots.txt /srv/www/curator/public_html/robots.txt
Alias /favicon.ico /srv/www/curator/public_html/favicon.ico
Alias /css /srv/www/curator/public_html/css
Alias /js /srv/www/curator/public_html/js
Alias /img /srv/www/curator/public_html/img
Alias /static/admin /srv/www/curator/public_html/admin
Alias /static /srv/www/curator/public_html/static
Alias /media /srv/www/curator/public_html/media
ErrorLog /srv/www/curator/logs/error.log
CustomLog /srv/www/curator/logs/access.log combined
</VirtualHost>
The Django docs mention an WSGIPythonPath directive, but I get an error that WSGIPythonPath cannot occur within <VirtualHost> section when I use this.
With this configuration I get a 500 error (well obviously, there's a lot of settings I haven't added yet for example) but not one that I would expect. I get this (IPs blanked by me):
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] mod_wsgi (pid=21802): Target WSGI script '/srv/www/curator/cmscore/cmscore/wsgi.py' cannot be loaded as Python module.
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] mod_wsgi (pid=21802): Exception occurred processing WSGI script '/srv/www/curator/cmscore/cmscore/wsgi.py'.
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] Traceback (most recent call last):
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] File "/srv/www/curator/cmscore/cmscore/wsgi.py", line 15, in <module>
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] from django.core.wsgi import get_wsgi_application
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] File "/srv/ve/.virtualenvs/curator/lib/python2.7/site-packages/django/__init__.py", line 1, in <module>
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] from django.utils.version import get_version
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] File "/srv/ve/.virtualenvs/curator/lib/python2.7/site-packages/django/utils/version.py", line 7, in <module>
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] from django.utils.lru_cache import lru_cache
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] File "/srv/ve/.virtualenvs/curator/lib/python2.7/site-packages/django/utils/lru_cache.py", line 28
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] fasttypes = {int, str, frozenset, type(None)},
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] ^
[Thu May 21 14:55:16 2015] [error] [client xxx.xxx.xxx.xxx] SyntaxError: invalid syntax
So, this appears to be an error which occurs when Django 1.8 is being using by an earlier version of Python than 2.7. To debug, I threw this into the top of my wsgi.py file:
import sys
activate_this = '/srv/ve/.virtualenvs/curator/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
sys.stderr.write(sys.version)
And in my Apache error log, I see:
2.6.6 (r266:84292, Dec 27 2010, 00:18:12)
I've tried every trick that I can think of (tricks that worked with existing virtualenvs of Django 1.6 and Python 2.7 on the exact same server!) including shebangs pointing to the correct Python install in my manage.py file, but I cannot find any way to get this to work.
I know I'm missing something really obvious here, but I am blind to it, can anyone help?

Error while configuring Django with mod_wsgi on Python inside Anaconda: "ImportError: No module named django.core.wsgi"

I'm having few problems running django+apache2 on ubuntu.
Django 1.7, Apache2.4.7
I'm running anaconda stack with 3.4 as main + another environment on python 2
I was trying to set up simple test site and received internal error 500, with below error messages in error log of Apache:
[Sat Jan 10 11:56:16.095032 2015] [:error] [pid 14441:tid 140015774267136] [client 127.0.0.1:59135] ImportError: No module named django.core.wsgi
[Sat Jan 10 12:03:01.895432 2015] [:error] [pid 14439:tid 140015774267136] [client 127.0.0.1:59157] mod_wsgi (pid=14439): Target WSGI script '/PATH/mysite/wsgi.py' cannot be loaded as Python module.
[Sat Jan 10 12:03:01.895483 2015] [:error] [pid 14439:tid 140015774267136] [client 127.0.0.1:59157] mod_wsgi (pid=14439): Exception occurred processing WSGI script '/PATH/mysite/wsgi.py'.
[Sat Jan 10 12:03:01.895498 2015] [:error] [pid 14439:tid 140015774267136] [client 127.0.0.1:59157] Traceback (most recent call last):
[Sat Jan 10 12:03:01.895515 2015] [:error] [pid 14439:tid 140015774267136] [client 127.0.0.1:59157] File "/PATH/mysite/wsgi.py", line 13, in <module>
[Sat Jan 10 12:03:01.895565 2015] [:error] [pid 14439:tid 140015774267136] [client 127.0.0.1:59157] from django.core.wsgi import get_wsgi_application
[Sat Jan 10 12:03:01.895582 2015] [:error] [pid 14439:tid 140015774267136] [client 127.0.0.1:59157] ImportError: No module named django.core.wsgi
wsgi.py:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
its running ok with
python manage.py runserver
So the issue here is (I think) mod-wsgi wasn't configured on correct python, as default python is ("mod_wsgi/3.4 Python/2.7.6 configured") and django is installed on anaconda 2.7.9 one.
If my guess is good (please let me know what other info would we need for soultion)
My question is: how do I recompile mod-wsgi so its linked against 2.7.9 environment from anaconda?
Another question would be, can I leave it as it is and just add some directive to link to anaconda libraries? Which folder would be that and where do I put WSGIPythonHome directive then?
Thank you for having a look
Had similar problem, answer was in virtual host file:
WSGIDaemonProcess mysite python-path=/path/to/mysite:/path/to/anaconda3/envs/python2/lib/python2.7/site-packages/
WSGIProcessGroup mysite
WSGIScriptAlias / /path/to/mysite/mysite/wsgi.py
ServerName www.mysite.com
ServerAlias mysite.com
ServerAdmin webmaster#mysite.com
<Directory /path/to/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog /path/to/web/test/error.log
Check this setting in httpd.conf of your Apache:
In your <VirtualHost x:y> section, make sure that value of python-path in WSGIDaemonProcess is including the Python site-packages directory in your Anaconda installation.
Othwerwise, mod_wsgi may not find Django that you've installed for Python inside Anaconda.
For example, if you've installed Anaconda to:
/opt/anaconda2
Python site-packages directory may be:
/opt/anaconda2/lib/python2.7/site-packages
Therefore, change WSGIDaemonProcess as:
WSGIDaemonProcess YOUR_PROJECT_NAME python-path=/opt/anaconda2/lib/python2.7/site-packages:YOUR_PROJECT_DIRECTORY ......
(Separate each path by colon :)
Note:
1. The version number within the path may change according to your tools' version. Please find similar path and try it.
2. This issue may happen on every Linux distribution (not only Ubuntu), even Windows.

Target WSGI script cannot be loaded as Python module + ImportError: No module named

I am having a an error while using mod_wsgi to deploy my flask app. This is the error log:
[Sat Feb 10 01:58:24.785611 2018] [wsgi:error] [pid 17184:tid 140591748056832] [remote 41.36.31.164:45065] mod_wsgi (pid=17184): Target WSGI script '/var/www/itemcatalog/itemcatalog.wsgi' cannot be loaded as Python module.
[Sat Feb 10 01:58:24.785635 2018] [wsgi:error] [pid 17184:tid 140591748056832] [remote 41.36.31.164:45065] mod_wsgi (pid=17184): Exception occurred processing WSGI script '/var/www/itemcatalog/itemcatalog.wsgi'.
[Sat Feb 10 01:58:24.785649 2018] [wsgi:error] [pid 17184:tid 140591748056832] [remote 41.36.31.164:45065] Traceback (most recent call last):
[Sat Feb 10 01:58:24.785664 2018] [wsgi:error] [pid 17184:tid 140591748056832] [remote 41.36.31.164:45065] File "/var/www/itemcatalog/itemcatalog.wsgi", line 6, in <module>
[Sat Feb 10 01:58:24.785710 2018] [wsgi:error] [pid 17184:tid 140591748056832] [remote 41.36.31.164:45065] from itemcatalog import app as application
[Sat Feb 10 01:58:24.785729 2018] [wsgi:error] [pid 17184:tid 140591748056832] [remote 41.36.31.164:45065] ImportError: No module named itemcatalog
I have an init.py file inside itemcatalog directory, I did some search and found that maybe that python doesn't identify my itemcatalog as a package, what is the reason for this?
Python was installed globally but then I also tried to also install it inside a virtual environment.
This is the code for the virtual host:
<VirtualHost *:80>
ServerName 18.194.244.229
ServerAdmin admin#18.194.244.229
WSGIDaemonProcess itemcatalog python-path=/var/www/itemcatalog:/var/www/itemcatalog/venv/lib/python2.7/site-packages
WSGIProcessGroup itemcatalog
WSGIScriptAlias / /var/www/itemcatalog/itemcatalog.wsgi
<Directory /var/www/itemcatalog/>
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And this is the file structure inside /var/www/itemcatalog/
Instead of:
WSGIDaemonProcess itemcatalog python-path=/var/www/itemcatalog:/var/www/itemcatalog/venv/lib/python2.7/site-packages
use:
WSGIDaemonProcess itemcatalog python-path=/var/www \
python-home=/var/www/itemcatalog/venv
The Python path must be the parent directory of the package, not the package directory itself.
Instead of using python-path to setup virtual environment, use python-home as explained in official documentation:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html

Categories