[mod_python] Re: mod_python and module importing

.: smilelover :. smilelover at centrum.cz
Thu May 11 04:15:59 EDT 2006


______________________________________________________________
> Od: smilelover at centrum.cz
> Komu:  <mod_python at modpython.org>
> CC:  
> Datum: 10.05.2006 11:04
> Předmět: mod_python and module importing
>
>  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?
> 
> 

OK, one of the problems was using os.getcwd() in PythonPath directive. It returns / and it returns / even if called directly in the script (index.py). It is not necessary to use it at all (mod_python adds the base script dir in PythonPath when index.py is called, doesn't it?).
When I added __init__.py with appropriate content to the lib dir, it worked.

Thanx for your advices, guys.
Dan



More information about the Mod_python mailing list