Numpy ImportError when deploying Flask App using mod_wsgi/Apache2 -
i'm trying flask app running on aws ec2 (standard ubuntu ami) through apache2 webserver. app internally uses numpy. have tested following:
without numpy, app runs through apache2. numpy, trips on
import numpy
, throws 500 server error (see logs below).with numpy, app runs fine when invoked directly command line (
i.e. python app.py
). numpy bits work expected , can query app endpoints externally.
i printed system path (print sys.path
) before import numpy np
, made sure appropriate site-packages directory in path. manually specified wsgipythonpath
include site-packages directory. tried setting number of threads 1 , editing apache2.conf file. none of these efforts have been successful.
below directory structure , contents of relevant files.
root folder /var/www/html/webserver_mockup
root folder contents
/var/www/html/webserver_mockup/app.wsgi
/var/www/html/webserver_mockup/mockup/__init__.py
/var/www/html/webserver_mockup/mockup/app.py
app.wsgi
(edited according graham's comment below)
#!/usr/bin/python import sys import site site.addsitedir('/home/ubuntu/.local/lib/python2.7/site-packages') sys.path.insert(0, "/var/www/html/webserver_mockup") mockup.app import app application
/etc/apache2/sites-available/000-default.conf
(edited according graham's comment below)
wsgipythonpath /usr/local/lib/python2.7/site-packages/ <virtualhost *:80> serveradmin webmaster@localhost documentroot /var/www/html wsgidaemonprocess webserver_mockup threads=5 wsgiscriptalias / /var/www/html/webserver_mockup/app.wsgi <directory /> wsgiprocessgroup %{global} wsgiapplicationgroup %{global} order allow,deny allow </directory> loglevel warn errorlog ${apache_log_dir}/error.log customlog ${apache_log_dir}/access.log combined </virtualhost>
apache error log
[fri apr 07 04:09:41.062282 2017] [mpm_event:notice] [pid 7221:tid 140325391972224] ah00489: apache/2.4.18 (ubuntu) mod_wsgi/4.3.0 python/2.7.12 configured -- resuming normal operations [fri apr 07 04:09:41.062358 2017] [core:notice] [pid 7221:tid 140325391972224] ah00094: command line: '/usr/sbin/apache2' [fri apr 07 04:09:46.770159 2017] [wsgi:error] [pid 7225:tid 140325250004736] [client 24.6.50.183:58332] mod_wsgi (pid=7225): target wsgi script '/var/www/html/webserver_mockup/app.wsgi' cannot loaded python module. [fri apr 07 04:09:46.770300 2017] [wsgi:error] [pid 7225:tid 140325250004736] [client 24.6.50.183:58332] mod_wsgi (pid=7225): exception occurred processing wsgi script '/var/www/html/webserver_mockup/app.wsgi'. [fri apr 07 04:09:46.770370 2017] [wsgi:error] [pid 7225:tid 140325250004736] [client 24.6.50.183:58332] traceback (most recent call last): [fri apr 07 04:09:46.770432 2017] [wsgi:error] [pid 7225:tid 140325250004736] [client 24.6.50.183:58332] file "/var/www/html/webserver_mockup/app.wsgi", line 7, in <module> [fri apr 07 04:09:46.770534 2017] [wsgi:error] [pid 7225:tid 140325250004736] [client 24.6.50.183:58332] mockup.app import app application [fri apr 07 04:09:46.770612 2017] [wsgi:error] [pid 7225:tid 140325250004736] [client 24.6.50.183:58332] file "/var/www/html/webserver_mockup/mockup/app.py", line 12, in <module> [fri apr 07 04:09:46.770693 2017] [wsgi:error] [pid 7225:tid 140325250004736] [client 24.6.50.183:58332] import numpy np [fri apr 07 04:09:46.770755 2017] [wsgi:error] [pid 7225:tid 140325250004736] [client 24.6.50.183:58332] importerror: no module named numpy
my question is: why apache2 not finding numpy despite appropriate site-packages being in path?
this:
site.addsitedir('/home/ubuntu/.local/lib/python2.7/site-packages/numpy')
should have been:
site.addsitedir('/home/ubuntu/.local/lib/python2.7/site-packages')
but whole way setting python virtual environments not best practice anyway. suggest read:
also try , switch daemon mode, better use daemon mode embedded mode.
Comments
Post a Comment