tags:

views:

23

answers:

1

I have a C++ module that I'm wrapping with SWIG that uses dynamic linking. Because of the way that python deals with scope of imported functions I've had to run the command dl.open(library, dl.RLTD_NOW, dl.RTLD_GLOBAL) directly after import. This is to make sure that the C++ libraries functions are available to the other libraries that it imports.

Of course what this means is that in order to import the module three lines are needed instead of one. However the other lines are constant and depend on nothing. That is I want to convert the lines:

import dl
import module
dl.open(library, dl.RTLD_NOW, dl.RTLD_GLOBAL)

into simply:

import module

I have tried looking through the SWIG documentation as to how to make it run code upon the import of the module, but I can't find anything. Is this possible to do?

Thanks.

A: 

Try wrapping your module. Build your C++ code into a "private" module, and call it module_ or something, to make it clear that you shouldn't import it. Then, in module.py (the wrapper module):

import dl
from module_ import *
dl.open(library, dl.RTLD_NOW, dl.RTLD_GLOBAL)
Jack Kelly