I have an Apache server with mod_wsgi, running an Python 2.7 script. The script uses the python Pillow module, installed via pip.
Running the script normally using python script.py
works okay, but when running the script from wsgi - An ImportError exception is thrown for PIL.
This is the Apache configuration from /etc/apache2/sites-enabled/000-default.conf
:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIScriptAlias /wsgi/ /home/nitay/Desktop/WebsitePath/Python/wsgi.py
<Directory "/home/nitay/Desktop/WebsitePath/Python">
Require all granted
</Directory>
</VirtualHost>
There's no virtualenv installed, and there's just one Python installation on this machine.
What can I do to make python find its installed modules?
I've seen solutions around the same ballpark that use mod_wsgi's daemon mode to manually define python path. Is there a way to do so in embedded mode?
EDIT: Apache error log:
[Wed Nov 02 16:08:02.931400 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] mod_wsgi (pid=48202): Target WSGI script '/home/nitay/Desktop/WebsitePath/Python/wsgi.py' cannot be loaded as Python module., referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931475 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] mod_wsgi (pid=48202): Exception occurred processing WSGI script '/home/nitay/Desktop/WebsitePath/Python/wsgi.py'., referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931557 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] Traceback (most recent call last):, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931601 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] File "/home/nitay/Desktop/WebsitePath/Python/wsgi.py", line 9, in <module>, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931687 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] import sprites, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931705 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] File "/home/nitay/Desktop/WebsitePath/Python/sprites.py", line 1, in <module>, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931767 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] from PIL import Image, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931830 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] ImportError: No module named PIL, referer: http://192.168.1.247/index.html
sys.path & version for the normal Python and WSGI:
Normal:
>>> sys.version
'2.7.11+ (default, Apr 17 2016, 14:00:29) \n[GCC 5.3.1 20160413]'
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/nitay/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0']
WSGI:
>>> sys.version
2.7.11+ (default, Apr 17 2016, 14:00:29) [GCC 5.3.1 20160413]
>>> sys.path
['/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib
I got stuck with this and the discussion above really helped. My solution however, was simply to set up the Python path at the beginning of my WSGI program, e.g.:
I redid the server configuration, this time naming things properly, used virtualenv, and Daemon mode to wsgi.
Here's the apache configuration I ended up with:
Moral of the story? "Time is precious, waste it wisely" (Don't half-ass server configurations)