Place Ghantoos

Apache mod_python local module import

Tagged:

I have searched a lot before finding this tip, so I post it.
It show how to easily import with mod_python a module based on a personal .py file located somewhere on your box.
In this example, I will try to import the module bloup.py located in the same directory of my index.py mainpage.
when trying to import using the usual means, i get:

#!/usr/bin/env python
import os
import bloup
...

>>>ImportError: No module named bloup

The solution I found was:

import os

from mod_python import apache

directory = os.path.dirname(__file__)
bloup = apache.import_module('bloup', path=[directory])

You can checkout my source: http://www.dscpl.com.au/wiki/ModPython/Articles/BasicsOfModuleImporting

Hope this helps,

Cheers,

ghantoos

Tagged:

2 Responses

  1. stephbul says:

    Thanks ghantoos, really usefull!

  2. moebiius_eye says:

    I don’t think your solution is the best one, let me show you how I do it:

    sys.path.append(“./”)
    import bloup

    It works fine for me.
    Please tell me if this is not a proper way to do it, since i am new to Apache’s mod_python.

Leave a Reply