0
votes

I can run successfully a "hello world " app with Flask + WSGI... But changing my project structure with the "routes.py" inside a "folder" makes the server giving error...

mod_wsgi (pid=9): Target WSGI script '/var/www/myfirstapp/hello.wsgi' cannot be loaded as Python module.

mod_wsgi (pid=9): Exception occurred processing WSGI script '/var/www/myfirstapp/hello.wsgi'

from folder.routes import simple_page

ImportError: No module named folder.routes

This is my project tree:

├── folder
│   └── routes.py
├── hello.conf
├── hello.py
├── hello.wsgi
└── README.md

hello.py:

from flask import Flask
from folder.routes import simple_page # works in dev but not with wsgi.. Why?


app = Flask(__name__)
app.register_blueprint(simple_page)

routes.py:

from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound

simple_page = Blueprint('simple_page', __name__,
                        template_folder='templates')

@simple_page.route('/')
def index():
    try:
        return "Hello world"
    except TemplateNotFound:
        abort(404)

hello.conf

<VirtualHost *>
    ServerName example.com
    WSGIScriptAlias / /var/www/myfirstapp/hello.wsgi
    WSGIDaemonProcess hello python-path=/var/www/myfirstapp:/var/www/myfirstapp/.env/lib/python3.5/site-packages
    <Directory /var/www/myfirstapp>
       WSGIProcessGroup hello
       WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

hello.wsgi:

import sys
sys.path.insert(0, "/var/www/myfirstapp")
from hello import app as application

Note: I don't get it why the WSGI is "fine" with that routes.py in the root folder (import routes), but complains about the (hello.py) import "folder.routes" in hello.py if I put that same file in the "folder"...

1
put an empty __init__.py file inside folder, see if that works.Paritosh Singh
Didn't work... :(user10047579

1 Answers

0
votes

I found a solution , referencing the respective module (folder) in WSGIDaemonProcess hello.conf line:

pythonpath=/var/www/myfirstapp/.env/lib/python3.5/sitepackages:/var/www/myfirstapp/:/var/www/myfirstapp/folder

Than in routes.py: instead of from folder.routes import simple_page we have import routes.

The project tree changed a little bit too:

├── myfirstapp
│   ├── folder
│   │   └── routes.py
│   └── __init__.py
├── hello.wsgi
└── README.md