[mod_python] [SPAM] [Newbie] import_module() function or Python's import statement

Guillaume Poletto polettog at gmail.com
Mon Aug 18 08:32:30 EDT 2008

Thanks for your answer

2008/8/18 Graham Dumpleton <graham.dumpleton at gmail.com>
> 2008/8/18 Poletto Guillaume <polettog at gmail.com>:
> >
> > Yes, I'm new to both Python programming language and mod_python
> If you are new to Python and want to write web applications, any
> reason why you didn't start out with one of the arguably more friendly
> higher level frameworks such as Django?

I don't know.. no doubt that Django is great, but i was seduced by
writing my own handler and controlling requests by parsing URIs by
Note that if i'm new to Python, i'm experimented in web developement,
and also in writing CGIs (although this is not CGI here, i know). I
already know some low level things concerning HTTP headers (i wrote
some tools in C to parse urlencoded query strings, POST data and
netscape cookies). So except the import_module() part, i did
assimilate most of the manual.
If there was no "util" module to fetch form parameters, i would have
taken a look to a higher level framework, but the base mod_python
seems to provide all i need :)
Also, i was interesting in writing Apache modules in C, so i thought
using mod_python at low level was good to learn about how work
handlers and apache internals

> > I made a test and i modified a module imported by "import" statement in my handler module : the changes where taken in count without restarting apache
> Two things could be happening here. The first is that the module being
> imported was in the same directory and was being managed by mod_python
> module importer and on detecting a change, it reloaded code
> appropriately.
> Alternatively, you have been deceived by the fact that
> Apache/mod_python on UNIX is a multiprocess web server and so a
> completely different processes which hadn't loaded it previously
> handled the request and so code got loaded for first time, and wasn't
> because of a reload at all. In this later case, it may not have even
> been a candidate for reloading and might just be a standard Python
> module/package.

That was it !
Yesterday, i made, the test 3 or 4 times, but there were 6 apache
processes, so at each request, the handler module was not yet
imported. This is why i thought modules where "reloaded".

> If you have:
>  PythonDebug On
> set in configuration and monitor the Apache error log files you should
> be able to see when a code files is being handled by mod_python module
> importer and when it is being first loaded and/or reloaded. If it
> isn't in there, it is a standard module/package and reloading not
> possible.

This is a great trick to look at the apache error log, i'll remember that

> If you are new to Python and have dropped down to the level of
> mod_python module importer already, I would suggest you run away.
> Instead have a look at one of the high level web frameworks for Python
> as you will be able to get onto developing your application much
> quicker and will not have to be worried about such low level details.
> :-)

Thanks for the advice but i think i'll look to a higher level
framework in a future project.
I've all the time !
Now i better understand what happens with "import" statements, i think
i will use them instead of import_module() and gracefully restart
apache at each update.
Now there's another problem : what's the right way to add another
import path ? Because i wanted to have the main source code outside
the directory in which is the handler module.
During my test, i did the tweak of adding dynamically my path to
sys.path list, but i find this a bit dirty.
So i found there was this apache directive "PythonPath" but according
to the manual, this is not recommanded to use it.
So what should i do?


More information about the Mod_python mailing list