[ACCEPTED]-Python's working directory when running with WSGI and Apache-mod-wsgi

Accepted answer
Score: 24

You should not change working directory.


import os
here = os.path.dirname(__file__)

The 8 variable here will then contain the directory 7 where that code file is located. You can 6 then construct absolute paths for things 5 relative to that.

database = os.path.join(here, 'database.db')

Do note that the user your 4 code runs under in Apache still needs read/write 3 access to that directory.

As always, make 2 sure you read the documentation. Relevant 1 sections of documentation are:

Score: 3

I had a similar problem where I wanted to 8 use a glob() with a relative path. It worked 7 in my development environment but not on 6 the server with mod_wsgi. I discovered here that 5 there is a 'home=' option that you can add 4 to the WSGIDaemonProcess directive to set 3 the initial working directory of your application. You 2 find that in the virtual host file (on my 1 system in /etc/apache2/sites-available/mysite.conf)

Score: 0

While add a path to relative directory works, probably 4 you need to change lot of code.

Change home 3 from WSGIDaemonProcess works but you need 2 to set one for each vhost

reset the cwd at 1 beginning of your script:

here = os.path.dirname(__file__)

for a quick workaround

More Related questions