0

When using Python under mod_wsgi, is there a way to know the path of the application script file (the .wsgi file) from inside a module?

GetFree
  • 34,030
  • 17
  • 70
  • 101
  • What is the use case for this? The immediate reason I can think you may want to do this there are other ways of doing it which may be better. – Graham Dumpleton Sep 03 '13 at 00:15
  • @GrahamDumpleton, I'm trying to identify which application is being executed so as to set the PythonPath automatically and not have to set it manually at the beginning of each .wsgi file. – GetFree Sep 03 '13 at 01:04
  • You would usually still do it in the WSGI script file, basing it on os.path.dirname(__file__). It really depends on how magic you are trying to make this. Certainly don't recommend you update sys.path from anywhere but the start of the WSGi script file, at least if doing it from Python code. Depending on how you are setting up Apache, can also be done in Apache if need be. Do you want to add an example to your question as to how you are doing it right now? – Graham Dumpleton Sep 03 '13 at 04:19
  • Right now I set the pythonPath using the `WSGIPythonPath` directive in Windows and the `WSGIDaemonProcess` directive in Linux. Unfortunately, the Windows approach doesn't work when you have several copies of the same wsgi applicaction. That's why I'm looking for a way that works everywhere. – GetFree Sep 03 '13 at 05:32
  • On Windows if you want to host multiple sites, you must do it in the WSGI script file. – Graham Dumpleton Sep 03 '13 at 09:36
  • @GetFree let me know if the answer helped, if not, I'll remove it. Thanks. – alecxe Jan 28 '16 at 18:53

1 Answers1

0

Not sure this option is the best, but will certainly work.

In you wsgi script set environ variable:

os.environ['WSGI_SCRIPT_FILE_PATH'] = __file__

Then, get the script path in your application modules.

Note, that this will work if you have one WSGI script per Python interpreter (thanks to Graham Dumpleton for the comment).

alecxe
  • 414,977
  • 106
  • 935
  • 1,083
  • 1
    Technically you could have a configuration where there are multiple WSGI script files being delegated to run in the same Python interpreter. If using an environment variable like this they would clobber the value of another if all tried to set it. Also, you don't need to use os.path.abspath(). The \_\_file\_\_ of the WSGI script file should already be absolute. – Graham Dumpleton Sep 03 '13 at 00:13