After some more thorough analysis, I think I understand what's going on here.
When Python starts up, it sets up the sys.path (all as part of initializing the interpreter).
At this time, the environment is used to determine where to find .pth files. If no PYTHONPATH is defined at this time, then it won't find your modules installed to sys.prefix. Additionally, since easy-install.pth is probably installed to your custom prefix directory, it won't find that .pth file to be parsed.
Adding environment variables to os.environ or sys.path after the interpreter has initialized won't cause .pth files to be parsed again. This is why you're finding yourself forced to manually do what you expect Python to do naturally.
I think the correct solution is to make sure the custom path is available to the Python interpreter at the time the interpreter starts up (which is before mysite.fcgi is executed).
I looked for options to add a PYTHONPATH environment variable to mod_fastcgi, but I see no such option. Perhaps it's a general Apache option, so not documented in mod_fastcgi, or perhaps it's not possible to set a static variable in the mod_fastcgi config.
Given that, I believe you could produce a workaround with the following:
- Create a wrapper script (a shell script or another Python script) that will be your new FastCGI handler.
- In this wrapper script, set the PYTHONPATH environment variable to your prefix path (just as you have set in your user environment which works).
- Have the wrapper script launch the Python process for your original fastcgi handler (mysite.fcgi) with the altered environment.
Although I don't have a good environment in which to test, I think a wrapper shell script would look something like:
#!/bin/sh
export PYTHONPATH=/your/local/python/path
/path/to/python /path/to/your/fastcgi/handler # this line should be similar to what was supplied to mod_fastcgi originally
There may be alternative workarounds to consider.
- From mysite.fgci, cause the Python to re-process the sys.path based on a new, altered environment. I don't know how this would be done, but this might be cleaner than having a wrapper script.
- Find an Apache/mod_fastcgi option that allows an environment variable (PYTHONPATH) to be specified to the fastcgi process.