[mod_python] mod_python module path

Graham Dumpleton grahamd at dscpl.com.au
Tue Nov 8 18:40:53 EST 2005


In terms of what might be considered preferred practice, especially when
using a multithreaded Apache MPM, if you feel that you must set sys.path
in your actual Python code, do it from a module imported using the
PythonImport directive. These imports only happen once and before
multiple threads for handling requests come into play. Thus no need
to check if it is already in sys.path.

If the Python modules you want to use are really a part of your web
application, as opposed to some generic modules, you might also consider
instead using apache.import_module(). This will allow you to specify
exactly where to load modules from at the time the call is made thereby
avoiding the issue of setting sys.path altogether.

If using apache.import_module() or PythonHandler on your own
modules in the document tree, always be aware though of the problems
that exist in mod_python in the area of module loading/reloading. See:

  http://www.dscpl.com.au/articles/modpython-002.html
  http://www.dscpl.com.au/articles/modpython-003.html

Graham


Nick wrote ..
> For performance reasons, it's always faster to do
> 
> if PYPATH not in sys.path: sys.path.append(PYPATH)
> 
> rather than to raise and handle an exception.
> 
> Nick
> 
> jelle wrote:
> > 
> > 
> > What works for me in the python file and prevents redundant path entries:
> > 
> > PYPATH = '/home/somegroovypath/my/py'
> > import sys
> > try: sys.path.index(PYPATH)
> > except: sys.path.append(PYPATH)
> > 
> > 
> > On Tue, 8 Nov 2005, Jorey Bump wrote:
> > 
> >> Pedro Ferreira wrote:
> >>
> >>> Hello all.
> >>> I'm starting with mod_python, and I'm thinnking about developing a
> >>> web application using it. However, I have some python packages of my
> >>> own, that i wanted to use, and don't know how to add them to the 
> >>> module path. I made a symlink in site-packages, to my module 
> >>> directory, and it works. However, I'll need to do it without 
> >>> administrator privileges. How is it possible? Is there any variable
> I 
> >>> should change?
> >>
> >>
> >> I use the PythonPath directive in my virtual host configuration:
> >>
> >> PythonPath "['/var/www/vhosts/www.example.com/python'] + sys.path"
> >>
> >> For more info, see:
> >>
> >> http://www.modpython.org/live/current/doc-html/dir-other-pp.html
> >> _______________________________________________
> >> Mod_python mailing list
> >> Mod_python at modpython.org
> >> http://mailman.modpython.org/mailman/listinfo/mod_python
> >>
> > _______________________________________________
> > Mod_python mailing list
> > Mod_python at modpython.org
> > http://mailman.modpython.org/mailman/listinfo/mod_python
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python


More information about the Mod_python mailing list