Importing a user module into your live WSGI application

authored by Frank Lynam at 08/02/2013 18:33:06

Here’s a quick tip if you want to use multiple modules in your Python WSGI application. Say for example, you write a module called library.py and you import this into your main.py file, which contains the WSGI application function (your app’s entry point). If you run this code within an IDE like Eclipse, it will work fine; you will be able to call functions defined in library.py from main.py. However, if you try to run the same code in a live Apache2 server you’ll notice that you get a HTTP Error 500, which is never nice to see.

The cause of the problem is that the WSGI module does not know where to look for the library.py file. You can sort this out by entering the following code at the top of your main.py module:


import os, sys
sys.path.append(os.path.dirname(__file__))
import library

The WSGI module will now know where to look for the library.py file.

Comments

submit