[mod_python] importing from a PSP file

Jorey Bump list at joreybump.com
Thu Jan 6 09:01:49 EST 2005


Simon Wittber wrote:

> I found that I cannot import modules which exist in the same folder as
> the psp file being served by apache.
> 
> eg:
> The file:
> 
> /home/simon/public_html/test.psp
> 
> cannot import:
> 
> /home/simon/public_html/package/module.py

No surprise, this doesn't work in Python, either.

> unless the path '/home/simon/public_html' is sys.path.appended at the
> top of the psp page.

Just make the subdirectory a package:

  cd /home/simon/public_html/package/
  touch __init__.py

Now, anything running from /home/simon/public_html/ can do this:

  import package.module

Obviously, you should choose unique names for your packages (and even 
modules) in order to avoid polluting the namespace.

> To get around this problem, I added the following lines:
> 
> cwd = str(os.path.split(self.filename)[0])
> sys.path.append(cwd)
> 
> under line 207 in '/usr/lib/python2.3/site-packages/mod_python/psp.py'
> which cleared up the problem.

Resist the temptation to alter mod_python if you're a newbie. Not only 
will you probably hose your installation, but noone will be able to help 
you if you are working from a nonstandard code base.

> Has anyone else come up against this problem? Does another (better)
> solution already exist somewhere else in mod-python?

See above, or simply put your module in the same directory as the 
importing module.

The same rules (generally) apply to mod_python as to running a single 
interpreter from the command line. Don't make the mistake of treating it 
like HTML, PHP, CGI, or anything else, for that matter. The embedded 
interpreter makes mod_python very powerful, and whenever people try to 
"fix" it to match their expectations, they tend to weaken it. Almost all 
of the problems you will encounter have a simple Python solution, and 
there is rarely an analog to the technology/language you're trying to 
replace.







More information about the Mod_python mailing list