[mod_python] Re: mod_python and module importing

Graham Dumpleton grahamd at dscpl.com.au
Thu May 11 06:17:18 EDT 2006


On 11/05/2006, at 6:15 PM, .: smilelover :. wrote:

> ______________________________________________________________
>> 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.

Add an __init__.py file into the "lib" directory doesn't make a great  
deal of sense
unless what you were actually doing was:

   from lib import MyWebTools

That is, "lib" was really acting like a package. If not, I don't see  
how it would make
any difference.

Graham


More information about the Mod_python mailing list