Re: Find location of module at runtime

Rokas Kupstys via Swig-user <[email protected]> Tue, 14 Jun 2022 09:10:44 +0300
Newsgroups gmane.comp.programming.swig
Message-ID <[email protected]>
You can do it by tapping into python import mechanism:

import os
import sys
import importlib
import importlib.util
import __main__

BASE_DIR = os.path.dirname(__main__.__file__)
sys.path.append(f'{BASE_DIR}/{__package__}')# Help SWIG bindings to find sibling modules. class SWIGModuleLoader(importlib.abc.Loader):
     def __init__(self):
         self._load_modules ='core', 'imgui', 'implot' def find_module(self, fullname, path=None):
         for modin self._load_modules:
             if fullname ==f'imgui_py.{mod}._{mod}':
                 return self def load_module(self, fullname):
         _, mod, _ = fullname.split('.')
         # TODO: Read it from env variable to support different cmake build configs. build_cache_dir ='cmake-build-debug' native_module_path =f'{BASE_DIR}/{build_cache_dir}/{__package__}/{mod}/_{mod}.so' # TODO: Windows/MacOS spec = importlib.util.spec_from_file_location(fullname, native_module_path)
         return importlib.util.module_from_spec(spec)


sys.meta_path.insert(0, SWIGModuleLoader())

-- Rokas Kupstys

On 2022-06-14 07:45, Rob McDonald wrote:
> 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

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