Python's working directory when running with WSGI and Apache

血红的双手。 提交于 2019-11-30 13:03:28

You should not change working directory.

Use:

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

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

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

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

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!