[mod_python] mod_python and module importing

Graham Dumpleton grahamd at dscpl.com.au
Wed May 10 07:02:43 EDT 2006


On 10/05/2006, at 7:04 PM, .: smilelover :. wrote:

> Hi guys,
> I have a simple python web running on mod_python, but I have  
> serious troubles with importing my own modules located in the  
> project directory.
>
> I have index.py and a directory called "lib", which contains a file  
> called MyWebTools.py.
> I have added the path to sys.path via:
>
> PythonPath "sys.path+[os.getcwd()+'/lib']"
>
> in .htaccess.
>
> But when I use "import MyWebTools", I get an error message saying  
> no such module exists a and it is the same when I place  
> MyWebTools.py directly into the project dir.
>
> The most weird I experienced is, that sometimes it works, somwtimes  
> not. And sometimes it works partialy - the module is imported, but  
> Python says that instances of a class contained in MyWebTools do  
> not have certain attributes (even though they DO have them and when  
> I declare the class in index.py, everything works fine).
> But most of the time it says the module is not there.
>
> So how can I safely import a module?

You can't say 'os.getcwd()' as there is no guarantees about what the
current working directory will be. Most Apache installations it will
actually return '/'.

Thus, you need to explicitly specify the full pathname to the directory
where MyWebTools resides.

Also note that MyWebTools must be a unique module name. If some
other web application on the server virtual host has a similar module,
you might pick that up by mistake. This may account for why you are
finding a version but not what you expected.

Graham


More information about the Mod_python mailing list