[mod_python] importing egg format libs installed on a custom path

Graham Dumpleton graham.dumpleton at gmail.com
Fri Sep 14 07:46:36 EDT 2007


On 14/09/2007, Graham Carlyle <graham.carlyle at maplecroft.net> wrote:
> On Fri, 2007-09-14 at 21:23 +1000, Graham Dumpleton wrote:
> > > The only problem is with eggs installed on a path outside site-packages
> > > that I'm pointing to with PythonPath. Seems I need to reference these
> > > sort of eggs explicitly on PythonPath to make them importable from the
> > > modpython handler. If the egg is installed in site-packages then it can
> > > be imported with no problem and no need to add the egg to the
> > > PythonPath.
> >
> > Set PYTHONPATH in the envvars file as well. That way it will be
> > present when Python first starts up and eggs will be activated
> > properly (I think).
>
> But then won't the eggs be available to all the modpython handlers? I
> wanted to have independence between the libraries used by different
> modpython handlers. So say I have an application that relies on version
> X of a library and another app that uses version Y of the same library.
> So each handler has a different PythonPath pointing to a different,
> potentially conflicting set of python modules, with some of these served
> by python eggs.

Then you need to use 'workingenv' or similar and trigger activation yourself.

Ie., do something like as described at the end of:

  http://docs.pythonweb.org/pages/viewpage.action?pageId=5439610

but adapt it to mod_python by putting it in a distinct Python code
file for each interpreter instance and using PythonImport to trigger
respective code files for each interpreter instance.

For example, if using mod_python 3.3.1, use:

  PythonImport /some/path/interp1.py interp1
  PythonImport /some/path/interp2.py interp2

At start of each .py file, set sys.path appropriately and then
activate working env.

Note though that using different versions of Python eggs, or any
module/package, can be dangerous if they contain a C extension module,
as an extension module is loaded only once per process and first to
load will win. This can result in Python wrappers for latter versions
not matching the inner C extension module resulting in errors or
crashes. Only choice to avoid that would be to use mod_wsgi daemon
mode or mod_fastcgi, presuming your application is a WSGI application
and you aren't using mod_python specific APIs.

Graham


More information about the Mod_python mailing list