Find location of module at runtime

Rob McDonald <[email protected]> Mon, 13 Jun 2022 21:45:42 -0700
Newsgroups gmane.comp.programming.swig
Message-ID <CAEppYpGpfpoGdNZP8dWxeVysSA=WWw8L=naK==hy21WiRcRYVg@mail.gmail.com>
I am wrapping my C++ library (foo) with SWIG.  My primary target is Python,
but occasionally use some of the other languages.

I need a way (from my module in C++) to determine where on the filesystem
the module is located.

Longer version:

My module needs to call another binary executable (bar).

The _foo.so (or _foo.pyd) and foo.py files including the bar (or bar.exe)
binary executable all need to be a part of a Python package that will be
installed via "pip install".

When I compile foo as a stand-alone executable, I have some code
"PathToExe()" that makes appropriate system calls to determine where the
currently running executable is located.  I then search the location of
foo.exe in order to find bar.exe -- and that is the version I call when
needed.

When the same code runs in Python, PathToExe() finds the path to the
currently running Python interpreter.  If you place bar.exe there, it finds
it and everything works.  However, I consider it bad form to require the
end user to place some binary files in the same location as the Python
binary.

Instead, I need to write some functions like...  'RunningAsModule()' and
'PathToModule()'  that I can use to set the path to bar.exe appropriately.

If this is impossible, I can probably use Python's 'inspect' to do
something like...

os.path.dirname( inspect.getfile( foo ) )

And then set the path in my module from the Python side.  However, it would
be a lot cleaner if the module could take care of this itself on startup.

Thanks for any suggestions,

Rob

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user