[mod_python] mod_python handlers seem to start in the wrong directory

Graham Dumpleton graham.dumpleton at gmail.com
Tue May 15 19:34:14 EDT 2007


When accessing files from a handler you should always be using
absolute paths and not relative paths. You cannot rely on the current
working directory being a specific value as any other handler or
module running under Apache might change the working directory. If
using a multithreaded MPM for Apache, ie., Windows or worker MPM on
Unix, the current working directory could change between the time you
set it and when you are relying on it being that value, thus making it
unreliable.

How exactly are you accessing the files in the same directory? What
does your code look like?

Note to determine what directory a Python code file is in, you can use:

  directory = os.path.dirname(__file__)

You can then use that as base directory to calculate absolute path for
other files in the same directory.

  file = os.path.join(directory, 'file.txt')

Graham

On 16/05/07, Adam Olsen <arolsen at gmail.com> wrote:
> Does anything look wrong with the following in my exaile.org virtualhost
> configuration?  Every once in a while, it seems as though the handler
> scripts are running in the wrong directory.  They can't find files that are
> in the same directory, even if I specifically use os.chdir() right before I
> try to access the file.
>
> <Directory /var/www/exaile.org/htdocs/plugins/>
>     AddHandler mod_python .py
>     PythonHandler plugins
>     PythonPath "sys.path +
> ['/var/www/exaile.org/htdocs/plugins']"
>     PythonDebug On
> </Directory>
> <Directory /var/www/exaile.org/htdocs/>
>     AddHandler mod_python .py
>     PythonHandler index
>     PythonDebug On
>     PythonPath "sys.path + ['/var/www/exaile.org/htdocs']"
> </Directory>
>
>
> --
> Adam Olsen
> http://www.vimtips.org
> _______________________________________________
> 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