pythonswig

Handle dependency in python file generated by SWIG


I have a swig generated python file like this :

from sys import version_info as _swig_python_version_info
if _swig_python_version_info < (2, 7, 0):
    raise RuntimeError("Python 2.7 or later required")

# Import the low-level C/C++ module
if __package__ or "." in __name__:
    from . import _FOO
else:
    import _FOO

Due to some reason which is not the context of this problem, I have a dependency that before import _FOO I need to have "import BAR" line. Otherwise the cpp library _FOO would not be loaded correctly. I cannot find a simple directive in SWIG which can do this. I have tried writing many things like %pythoncode import BAR at the top of the SWIG interface (.i) file, still it only writes after this import _FOO line. Please give me a solution to this.


Solution

  • Use the %pythonbegin directive to add code to the beginning of the Python code.