[mod_python] importing modules in PSP

Graham Dumpleton grahamd at dscpl.com.au
Mon Jan 30 17:34:04 EST 2006


.: smilelover :. wrote ..
> Hi, another question ;)
> in classic CGI I can import a .py file that lies in the same directory
> as  
> an ordinary module. e.g. if I have files index.py and foo.py, I can put
> "import foo" in index.py and it works.
> Nevertheless, in PSP it doesn't work :( 

Where within your Apache configuration did you include
"PythonHandler mod_python.psp"? Have you overriden the
PythonPath setting for mod_python?

Provided that the PythonHandler directive was defined within
the context of a Directory directive or in a .htaccess file and
you did not override PythonPath directive, then the directory
that the PythonHandler directive was specified for, should
have been automatically added to sys.path. When this occurs,
if your "foo.py" is in that PythonHandler directory, your
"import" should have found the module.

If you use Location directive, put PythonHandler directive
for .psp files at global context, or override PythonPath, then
this is not going to work.

Either way, you might simply be better of using something
like:

  import os
  from mod_python import apache
  directory = os.path.dirname(req.filename)
  foo = apache.import_module("foo",path=[directory])

Ie., in place of saying "import foo". Using the longer approach
you can say exactly where the "foo" module is located that you
want to import and it does not matter that the directory is not
in sys.path.

Graham


More information about the Mod_python mailing list