Saphire
Saphire

Reputation: 1930

How to correctly add paths for wsgi?

I have a web-app written in python and to deploy and use it on public_html I have a short Main.wsgi with this content:

#!/usr/bin/python
# -*- encoding: utf-8 -*
import sys, os
from werkzeug.wrappers import Request
from PageManager import PageManager
import werkzeug.contrib.sessions as sessions

fsstore = sessions.FilesystemSessionStore()

sys.path.append(os.path.dirname(__file__))
os.chdir(os.path.dirname(__file__))

@Request.application
def app(request):
    pm = PageManager()
    session = request.environ["werkzeug.session"]
    return pm.processPage(request)

#application = app
application=sessions.SessionMiddleware(app,fsstore)

if __name__ == "__main__":
    from werkzeug.serving import run_simple
    run_simple("localhost", 8080, application,
               use_reloader=False, use_debugger=True)

Now the thing is, when I try to navigate to this .wsgi I get following error:

ImportError: No module named PageManager

This is the folder structure

WebApp_1/
├── aufg1
│   ├── ...
├── aufg2
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── Main.py
│   ├── Main.pyc
│   ├── PageRenderer.py
│   ├── PageRenderer.pyc
│   ├── SQLSecurity.pyc
│   └── Testing.py
├── GeneralTools
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── Main.wsgi
│   ├── PageManager.py
│   ├── PageManager.pyc

Upvotes: 1

Views: 85

Answers (1)

jaime
jaime

Reputation: 2334

You should package your application up in a package. So that the hierarchy looks similar to:

webapp1
  |
  +---- __init__.py
  |
  +---- aufg1 (python package)
  |       |
  |       +-- ...
  +---- aufg2
  |       |
  |       +-- __init__.py
  |       +-- all your other modules.py
  +---- generaltooks
          |
          +-- ...

You should also break the habit of naming your packages and modules using CamelCasing. Instead, convert them to lowercase - generaltools, pagemanager, etc.

Next, update your imports like so:

import sys, os
from werkzeug.wrappers import Request
from webapp1.generaltools.pagemanager import PageManager
# If you do not rename your modules to lowercase, then use this import
# instead of the one above.
from WebApp_1.GeneralTools.PageManager import PageManager
import werkzeug.contrib.sessions as sessions

Then run your script. You will need to be a directory above the package webapp1 (in order to be able to import webapp1).

Upvotes: 1

Related Questions