[mod_python] Re: mod_python and module importing

.: smilelover :. smilelover at centrum.cz
Thu May 11 06:57:00 EDT 2006


______________________________________________________________
> Od: grahamd at dscpl.com.au
> Komu:  ".: smilelover :." <smilelover at centrum.cz>
> CC:  <mod_python at modpython.org>
> Datum: 11.05.2006 12:17
> Předmět: Re: [mod_python] Re: mod_python and module importing
>
>  
> 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</mod_python at modpython.org>

Yes, I want it to be a package and that's why I added __init__.py and use the "from ..." stuff. When I change the import command to

    import MyWebTools

it works only sometimes (when reloading the page for several times), it's strange.
I think it shouldn't work at all, because there is no standalone module called MyWebTools in the base directory and no package called MyWebTools (just the lib pack).

Dan



More information about the Mod_python mailing list