1

I have my flask app working just great with the dev server when I run it like this:

python manage.py runserver

But when I try to run it with wsgi I get all kinds of "module not installed" errors, and even syntax errors. Strangely, it shows a different error each time I hit an app route.

on some of the errors, from the output in the apache error log, I can see that it's trying to run 2.7 versions of packages, when I am using 3.4.

File "/usr/lib/python2.7/dist-packages/requests/__init__.py", line 55, in <module>

I don't know why this is, python 3 is the default on the server and

$ python --version
Python 3.4.2

is the output for every user on the system. I am using virtualenv, but I did follow these instructions for setting up mod_wsgi with virtualenv: http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/

my wsgi file looks like this:

import sys

activate_this = '/home/flask-dev/es_app/venv/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

print(sys.path)

from searchapp import app as application

and that print(sys.path) outputs this when I run it like 'python run.wsgi':

['/home/flask-dev/es_app/venv/lib/python3.4/site-packages', '/home/flask-dev/es_app', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/lib/python3/dist-packages']

i.e. a path to the 2.7 packages is nowhere in sight. Also, all the required modules are installed in my virtualenv, which is why it works fine when I run the dev server.

Totally stumped on this. Any help appreciated.

davidism
  • 121,510
  • 29
  • 395
  • 339
tarponjargon
  • 1,002
  • 10
  • 28

1 Answers1

1

You need to tell Apache which Python executable it should use to launch python processes. Without explicit configuration, Apache probably takes the first 'python' present in the system path. (In fact no, see comments below. The distribution default python is used.)

Unfortunately, it looks like you need to re-compile mod_wsgi, as the python exe can only be changed by the configure script.

http://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html#multiple-python-versions

In your case: ./configure --with-python=/home/flask-dev/es_app/venv/bin/python

That's a lot of work for something that should be simple. Usually i just use Apache in reverse-proxy mode (thanks to mod_proxy), and point it to an external WSGI server like Gunicorn (http://gunicorn.org/) or Waitress (http://docs.pylonsproject.org/projects/waitress/en/latest/).

Stephane Martin
  • 1,612
  • 1
  • 17
  • 25
  • Good call on recompiling the module - I totally forgot about that. Yes, this whole thing should be simpler. But I guess that's why flask is still v0.11.I like your idea of using uWSGI, I was curious to know if there's also a resource advantage (or disadvantage)? – tarponjargon Dec 12 '16 at 22:26
  • uWSGI ? No i did not mentioned it :) But yes it is another possibility. In all cases you have Apache process and python processes. With Apache + mod_wsgi they are tightly coupled. With mod_proxy + an external WSGI server they are not. I think the consumed resources depend more on the configuration tuning (how many process/threads) than on the kind of WSGI you use. – Stephane Martin Dec 12 '16 at 22:32
  • 1
    Your description is incorrect. When using mod_wsgi it doesn't use the first 'python' present in the system path. Instead mod_wsgi is compiled for a specific Python version and links directly to the Python shared library. The solution is indeed though to reinstall mod_wsgi with a version which is compiled for the Python version that you want to use. Usually a Linux distribution has this for main stream Python versions it provides, so all you need to do is uninstall the mod_wsgi version for Python 2 and install that for Python 3. – Graham Dumpleton Dec 12 '16 at 23:18
  • OP should read http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html which talks about how to use virtual environments correctly with mod_wsgi and warns about mixing Python versions. As to installing from source code, for easy way of installing mod_wsgi see https://pypi.python.org/pypi/mod_wsgi Just use ``pip install`` and then ``mod_wsgi-express module-config`` to get lines to add to Apache configuration to load module. Ensure you uninstall system mod_wsgi module first. – Graham Dumpleton Dec 12 '16 at 23:22
  • 1
    Finally, you can always use ``mod_wsgi-express`` from the command line and/or proxy it behind a front end web server. Is no different to using gunicorn or uWSGI. – Graham Dumpleton Dec 12 '16 at 23:28
  • Thanks graham. I think re-compiling mod_wsgi was the missing piece. I went ahead and did that. I am still getting errors, but they are path-related, which I think I can debug. I do have a question...if I include a print statement in my wsgi file (like I did), how can I easily see the output when the server runs it? do I need to load a logger? – tarponjargon Dec 13 '16 at 00:37
  • every answer is there: http://modwsgi.readthedocs.io/en/develop/user-guides/debugging-techniques.html – Stephane Martin Dec 13 '16 at 00:57
  • Anything output to ``stdout`` or ``stderr`` via ``print()`` or the ``logging`` module, if it is configured to send to ``stdout`` or ``stderr`` (as is recommended under mod_wsgi), will go to the Apache error log. – Graham Dumpleton Dec 13 '16 at 01:38
  • cool - thanks again. Got it all worked out! thanks for your work on this module. – tarponjargon Dec 13 '16 at 03:33