[mod_python] Unable to import modules in subdirs

Jorey Bump list at joreybump.com
Fri Oct 28 11:39:27 EDT 2005

Brandon N wrote:
> Are you forced, with mod_python, to write all package code within the 
> __init__.py? I ask because of what you've mentioned a few times now and 
> what I keep encountering.

No. This isn't a mod_python issue, the first step in creating a Python 
package (as opposed to module) is to create a directory that contains a 
__init__.py file. This file can be empty, but I generally put my main 
code in it, and create parallel modules for separate importing. Let's 
say my module search path contains /var/www/python. The contents might 
look like this:

/var/www/python/apps/__init__.py (empty file)
/var/www/python/apps/bar/__init__.py (main code for application)
/var/www/python/apps/bar/db.py (db connection and functions for app)

So I can use import statements like:

  # import a "normal" module
  import foo
  # import a package
  import apps
  # importing apps doesn't automatically import apps.bar
  import apps.bar
  # importing apps.bar doesn't automatically import apps.bar.db
  import apps.bar.db

Note that you can also do this, as long as there are no namespace 

  from apps import bar

But I like to preserve dotted notation for clarity, and it sometimes has 
the side effect of offering more security (against traversal exploits, 
for example).

Python packaging is an advanced subject, and not well documented. This 
is only the tip of the iceberg, look at the standard library to get an 
idea of how the experts do it. Sorry to bore you with a primer if you're 
already aware of these issues.

> I have Vampire up and running, though from a doc it specifies "...The 
> mechanism will also not work where a module is actually a package. Any 
> application specific modules should therefore not be structured as 
> packages..." which I imagine is a restriction coming from mod_python. I 
> have existing code that is already structured in various packages and 
> I'd like to keep it that way.

This is handler-specific. Graham doesn't like packages. :)

> I'm able to import Admin but Admin.RemoteAdmin fails (as before). The 
> only way I've found around this is via an import of RemoteAdmin within 
> Admin's __init__.py which isn't much of a fix.
> With that klunky method I get the desired:
> import Admin.RemoteAdmin as RA
> A = RA.RemoteAdmin( )
> Is there a more pythonic way to accomplish this from within mod_python?

Try Publisher. I still need to test my apps against 3.2, but older 
versions respect package imports (I'm in *big* trouble if 3.2 breaks this).

Note that Graham's put a lot of work into Vampire to address some of the 
  stumbling blocks encountered by mod_python.publisher users. You may 
welcome his approach after comparing the two. There's no doubt that they 
both have caveats unlike anything you'll encounter in a PHP-like 
environment. This is also true of mod_python.psp and just about any 
other handler/framework written for mod_python. But you couldn't pay me 
to go back to PHP after developing web applications in Python (well, you 
could, but I'm going to charge double). :)

More information about the Mod_python mailing list