[Web-SIG] cwd and mod_wsgi

Graham Dumpleton graham.dumpleton at gmail.com
Sun Dec 16 10:21:17 CET 2007


On 16/12/2007, Massimo Di Pierro <mdipierro at cs.depaul.edu> wrote:
> Does anybody know how to change the current working directory in
> apache with mod_wsgi?

Why do you want to be able to change the working directory?

Generally it is not a good idea to be writing a WSGI application which
depends on being executed within a specific current working directory.
This is because there is no guarantee across different web hosting
environments that one can control the application working directory
and if you try to change it yourself from the application code then
you may run into issues with multithreading and different parts of an
application wanting it to be set to different values at the same time.

Thus, as explained in the Apache mod_wsgi documentation, specifically
section "Application Working Directory" of:

  http://code.google.com/p/modwsgi/wiki/ApplicationIssues

you should aim to always use absolute paths to access the file system.
To make an application somewhat moveable, you can calculate the
absolute path based relative to the __file__ attribute of a Python
module.

That said, if running in daemon mode of Apache mod_wsgi, you can
control the home directory of the application using the 'home' option
to WSGIDaemonProcess. In Apache mod_wsgi 2.0, daemon processes will
actually by default change to the home directory of the user the
process is running as if that directory isn't overridden through that
option. For details of these options see WSGIDaemonProcess directive
documentation in:

  http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives

In embedded mode of Apache mod_wsgi there is no way to specify through
the configuration what the application working directory should be, as
the processes are shared by other Apache modules and because it is a
shared environment, it would be bad practice to be trying to change
the working directory as something else could easily change it and
your application would fail if it was dependent on the working
directory remaining the same.

BTW, for Apache mod_wsgi questions are better directed to the official
mod_wsgi list at:

  http://groups.google.com/group/modwsgi

The reference to the list and all the documentation can be found at:

  http://www.modwsgi.org

Graham


More information about the Web-SIG mailing list