[mod_python] import_module

Kamil Niechajewicz kamil at nvstudio.pl
Wed Apr 14 15:29:58 EST 2004

Okay, I quite see the problem with importing wrong modules, where
there are two or more dirs with modules named the same (like just
simple index.py which is default module for Publisher)

The problem is there is no namespace for different 'modules' - 
here I mean directories. I will provide a sample app for easier

   index.py          - http://localhost/  = main page
       index.py      - http://localhost/admin      = admin panel
       tree.py       - http://localhost/admin/tree = show tree of objects

We put a single Apache directive:

<Directory /app_root>
	SetHandler mod_python.publisher
	... other statements here, I will ommit them because everyone
        probably knows what goes into such declaration

Now the test:

1. We enter site at url http://localhost/  -> page shows correctly content
generated by /app_root/index.py. The module index.py is loaded.

2. We enter site at url http://localhost/admin  -> page shows correctly 
content generated by /app_root/admin/index.py 

3. We enter site at url http://localhost/ again -> page shows content of
/app_root/admin/index.py, because reimporting module 'index' does not
occur - module_import checks that module is loaded, checks its mtime and
decides not to reimport it, so we end with 'index' module from a wrong

4. We can reload this page a few times - it still shows wrong content,
because module is not reloaded, but after some time it reloads it and
again shows what should be shown.

There is an ad hoc solution - just force import_module() to reimport
modules every time, but it is not very efficient ;) 

The best solution would be changing Publisher (or maybe even apache.py)
to somehow recognize 'packages' - in the above sample our packages 
could be named 'default' (for /app_root/*) and 'admin' (for /app_root/admin).
This would lead to modules named default.index and admin.index, so there
will be no place for mistake because we will have usunual namespace for
every directory.

Actually it works strange, because it only recognizes 'modules' from
the path in url, like 'some/package/index' is module 'index' for it,
and 'some/other/package/index' is also 'index'. sys.path has something
to do here, but anyway, it should be like some.package.index and 
some.other.package.index when importing them by Publisher, because 
only that will help to recognize proper modules with the same name
in the module list.

Am I right?


More information about the Mod_python mailing list