I've just deployed my Flask application on Apache2 with mod_wsgi. However, to do that successfully, I had to modify my application code. The change was with respect to directories. I found that by running os.getcwd()
and os.listdir(cwd)
, the application was running in root (/).
Because of this, I could not use any relative path names to access files. For example, earlier I was accessing secret files using open("../secrets/app_secrets.json")
. Since my application now runs in /, I am forced to identify files using the absolute path names. It works, but is highly inconvenient because the absolute file paths differ from my server, local directories, and others using my work. So I guess my question is:
- Why is this a good design? I can't imagine why mod_wsgi runs python files from "/". In fact, how it does this is beyond me.
- Is there any way I can change this behaviour and make the python file run from a specific folder so I can use relative paths?
Edit: Ok, I have read the link provided by Graham in the comments (https://modwsgi.readthedocs.io/en/develop/user-guides/application-issues.html#application-working-directory) but I can't wrap my head around this: why would using relative paths be such a bad idea? My app uses a lot of file IO and I'd hate to have everything in absolute paths: I have a lot of environments and modifying code one a single environment will not be easy to replicate across the other ones. Is there no easy way to do this apart from using absolute paths?