[mod_python] sys.path

Deron Meranda deron.meranda at gmail.com
Fri Aug 11 11:48:23 EDT 2006

On 8/10/06, Graham Dumpleton <grahamd at dscpl.com.au> wrote:
> luby liao wrote ..
> > After I installed mod_python and tested it, it did not work.  I got a
> > 'Internal Server' error.  The cause was that sys.path was incorrect.  I
> > used
> > /usr/local/bin/python2.3 to build, but sys.path did not include
> > /usr/local/lib/python2.3..., instead, it included sub-directories of
> > /usr/lib/python2.3.  This problem is easily fixed by setting up soft links.
> > But I am curious as to how the build process determines the sys.path.
> By some strange magic that I don't even fully understand.
> In short, when Python is initialised by mod_python at run time, Python does
> various checks to try and work out where everything is installed and from
> that setup sys.path. One of the prime ways it uses to determine this is to
> look at which 'python' executable is found in the PATH of the user which
> Apache is executed as. If it can't find one, it possibly falls back to some
> standard defaults (not sure).
> End result is that it appears that the PATH of the user that Apache runs as
> does not include '/usr/local/bin' towards the head and thus doesn't find the
> version of Python you actually used to build mod_python. Thus, add that
> bin directory to the start of PATH for user Apache runs as or in the Apache
> startup scripts, whatever may be appropriate for your platform.

Yes, it is a lot of strange magic!

Rather than alter PATH, you may be able to just set the PYTHONPATH
variable instead.  That way you won't affect any non-python stuff.

If you configured your python with just --prefix, then just set PYTHONPATH
to that directory.  If you however separated the platform-specific files
by using --exec-prefix as well, then PYTHONPATH should consist of
the prefix path, a colon, and the exec-prefix path.

I'm not sure myself why it is picking up the wrong python.  Perhaps
there is something that the mod_python configure process should
be doing???  However all mod_python users should be aware that it
is not possible to use Apache + mod_python with more than one
python version at the same time.  Although you can certainly use
multiple interpreters, they are all the same python.

If you're curious to dive deeper (perhaps Graham?) and have the python
source code, the initial paths for loading modules is determined by the
Py_GetPath() function in the source file Modules/getpath.c.  There's
lots of comments in that file that try to explain how things work.  This
path setting is done very early in the Py_Initialize() function, and
is what is used before the sys module itself is loaded and/or any of
the sys.path or importer hooks can even be changed.
Deron Meranda

More information about the Mod_python mailing list