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

Graham Dumpleton graham.dumpleton at gmail.com
Sun Aug 17 20:59:19 EDT 2008

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?

> and i found the import_module() paragraph in the manual not really easy to eat.

It is a complicated issue. :-)

> What I understood is that, a such function is necessary to reload modified modules used by the webserver because the interpreter lives as long as the apache child process lives.

It isn't absolutely necessary, it is a convenience more for when
developing code as you don't need to restart Apache after every
change. Do note though that the code modules this applies to is quite
limited. For changes to modules/packages installed on sys.path you
still need to restart the whole of Apache.

> Okay, but what was not clear is : does mod_python overrides the Python's statement "import" to make the same things than import_module()?
> If yes, why this function?

It doesn't strictly override 'import' statement. Instead it hooks in
Python APIs that underlay 'import'.


You thus have the ability to say that for certain imports they are
treated in certain ways and taken from different storage mechanisms.

> 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

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

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

> Maybe the difference is in the treatment of packages.. The import_module() breaks all namespace stuff with packages (we could not do the same as "import package.subpackage.module" and then use stuff with "package.subpackage.module.function()" notation, although this doesn't seem to be recommanded in classic python softwares)

The documentation says that it only applies to single Python code
files and doesn't work for Python packages. If you are trying to get
it to work for Python packages, at least to the extent of expecting
reloading to work, you will be disappointed.

> As I am new to Python, i was first thinking of designing my website like a classic python application, with packages and stuff, but tell me i should rather think of arranging my modules in classic directories and use import_module() each time.

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.


More information about the Mod_python mailing list