[mod_python] how to configure sys.path when installing not into python's lib directory?

Graham Dumpleton graham.dumpleton at gmail.com
Mon Jul 27 22:31:56 EDT 2009

2009/7/28 Marc Weber <marco-oweber at gmx.de>:
> Hi
> I'd lite to play with mod_python. I'm running nixos using the nix
> package manager. It's special because every package is installed into
> its own package diroctory by default.
> It compiles fine but of course the import path is not correct then:
>  [Tue Jul 28 00:59:43 2009] [error] make_obcallback: could not import mod_python.apache.\n
>  ImportError: No module named mod_python.apache
>  [Tue Jul 28 00:59:43 2009] [error] make_obcallback: Python path being used "['/nix/store/axq79wn5py4sfi2n142jfc4arvgyr957-python-2.5.4/lib/python25.zip', '/nix/store/axq79wn5py4sfi2n142jfc4arvgyr957-python-2.5.4/lib/python2.5', '/nix/store/axq79wn5py4sfi2n142jfc4arvgyr957-python-2.5.4/lib/python2.5/plat-linux2', '/nix/store/axq79wn5py4sfi2n142jfc4arvgyr957-python-2.5.4/lib/python2.5/lib-tk', '/nix/store/axq79wn5py4sfi2n142jfc4arvgyr957-python-2.5.4/lib/python2.5/lib-dynload', '/nix/store/axq79wn5py4sfi2n142jfc4arvgyr957-python-2.5.4/lib/python2.5/site-packages']".

Where did the Python modules for mod_python get installed to? By
rights they should still have been installed into:


unless the Python installation has been mightily fiddled with as well.

> I read from docs that I can use
>  PythonOption mod_python.importer.path "['path']"
> but this didn't work? Is it added after the lines
>    m = PyImport_ImportModule("mod_python.apache");
> are executed (mod_python.c)

That option is totally unrelated, Ignore it.

> The README also contains a hint:
>  - Running your own version of mod_python without installing it is possible and
>  useful if you cannot be root, but you're a bit on your own, sorry. You'll have
>  to make sure MOD_PYTHON/testconf.py points to the right version of
>  mod_python.so and tweak Apache user's PYTHONPATH environment variable so that
>  mod_python can find its Python modules.
> What is the Apache user's PYTHONPATH environment variable?
> I tried adding to my apache config:
>  SetEnv PYTHONPATH $PACKAGE_DIR/nix/store/axq79wn5py4sfi2n142jfc4arvgyr957-python-2.5.4/lib/python2.5/site-packages

You can't use SetEnv to control environment variables that Python
might be able to use at initialisation time.

What is $PACKAGE_DIR anyway? Isn't the Python installation under:


Why is another prefix needed?

If Python isn't under that directory, then likely that Python command
line wouldn't work either.

> A php test "echo getenv('PYTHONPATH');" showed that the var got assigned.
> Still the path isn't added to the import list.

Also, all that stuff you quote relates to running test scripts for
mod_python from the command line, not an installed installation.

> Is there something else I can try?

If all you want to do is host a WSGI capable Python application, then
mod_wsgi will likely be a lot easier to get working as it doesn't have
dependencies on installed Python code files for it itself to work. You
can also with mod_wsgi set an Apache configuration directive to tell
it where Python installation is, but then that shouldn't be required
unless the Python installation has been totally stuffed up with this
packaging scheme.

> Of course I can patch the python install receipt making it install
> mod_python as well. However that's suboptimal.
> Can I automatically patch the .c code to add that module directory?

Sounds like the wrong way of going about it.

State where the Python modules for mod_python are installed and what
is required may be more obvious.


More information about the Mod_python mailing list