5

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?

4
  • 1
    Relying on what the current working directory is is a bad practice for web applications. See modwsgi.readthedocs.io/en/develop/user-guides/… as to what you should do. Commented Jul 9, 2019 at 0:59
  • 1
    Put the one absolute path in a variable and it doesn’t matter how many places you need it. Commented Jul 9, 2019 at 3:09
  • @GrahamDumpleton if you expand your comment I will accept it as an answer!
    – a3y3
    Commented Mar 11, 2020 at 18:04
  • 1
    This is something I am currently struggling with as well! Commented Aug 20, 2020 at 2:20

0

Browse other questions tagged or ask your own question.