[mod_python] Unable to import modules in subdirs

Jorey Bump list at joreybump.com
Thu Oct 27 15:35:33 EDT 2005


Brandon N wrote:
> Hello all,
>    I am new to mod_python and would like to switch some existing code of 
> mine away from php. I'd be very happy to get up and running with python.
> 
> Unfortunately, I've run into a snag or two. I currently have an apache 
> httpd.conf of the following:
> 
> <Directory /var/www/localhost/htdocs/py>
>         SetHandler mod_python .py
>         PythonPath "['/var/www/localhost/htdocs/py', 
> '/var/www/localhost/htdocs/py/Admin']+sys.path"

Don't do that. Create a directory outside of the DocumentRoot for your 
own module repository.

>         PythonHandler mptest
>         PythonDebug On
> </Directory>
> 
> And a directory of:
> 
> /var/www/localhost/htdocs/py/
> Admin/  LocalAdmin.py  __init__.py  mptest.py
> 
> __init__.py's contents:
> __all__ = [ "Admin" ]
> 
> /var/www/localhost/htdocs/py/Admin/
> Admin.py  __init__.py

You've turned this directory into a package, so when you import Admin, 
it is actually importing /var/www/localhost/htdocs/py/Admin/__init__.py, 
not /var/www/localhost/htdocs/py/Admin/Admin.py.

> __init__.py's contents:
> import sys
> sys.path.insert( 0, '../' )

?

> both Admin and LocalAdmin have similar code of:
> class LocalAdmin( object ):
>     def __init__( self ):
>         self._var = -1
> 
>     def var( self ):
>         return self._var
> 
> Though Admin defines the class as RemoteAdmin
> 
> mptest.py is:
> from mod_python import apache
> import LocalAdmin
> import Admin

With your current scheme, you'd have to import Admin.Admin to get an 
Admin.Admin.RemoteAdmin object.

> def handler(req):
>     req.content_type = 'text/plain'
>     req.send_http_header()
> 
>     LA = LocalAdmin.LocalAdmin( )
>     #A = Admin.RemoteAdmin( )
> 
>     req.write( "%s\n" % LA.var())
>     for l in dir( Admin ):
>         req.write( "%s\n" % l )
>     return apache.OK
> 
> This code works, with the output of
> ---
> 
> -1
> __all__
> __builtins__
> __doc__
> __file__
> __name__
> __path__
> sys
> 
> ---
> So RemoteAdmin is not seen, though I can in fact import Admin...
> 
> Uncommenting the A = ... line throws an expected exception given the dir 
> results:
> 
>   File "/var/www/localhost/htdocs/py/mptest.py", line 17, in handler
>     A = Admin.RemoteAdmin( )
> 
> AttributeError: 'module' object has no attribute 'RemoteAdmin'
> 
> 
> Everything in the py and py/Admin have permissions:
> rwxr-xr-x
> 
> Any clues as to what I'm missing?
> 
> Thanks in advance for any insight!

Do this instead:

create /var/www/localhost/python

Then:

  /var/www/localhost/python/LocalAdmin.py
  /var/www/localhost/python/Admin/__init__.py

You want Admin/__init__.py to resemble LocalAdmin.py (but with your 
RemoteAdmin class, etc.). Of course, you could just create Admin.py at 
the same level as LocalAdmin.py, but this primitively demonstrates how 
to make a package.

Change your PythonPath to:

  PythonPath "['/var/www/localhost/python'] + sys.path"

Now your mptest.py should work without modification.

HTH, YMMV, untested, exchanges only, NO REFUNDS. :)



More information about the Mod_python mailing list