3
votes

I am writing a C++ extension for Python using SWIG. From what I understand of Python and SWIG, each module must have its own .pyd file. For example, if I have a module named 'mymodule', there should be a corresponding '_mymodule.pyd' file.

In my particular case, I would like to have only one 'pyd' file and multiples modules linking to it.

mypackage/
mypackage/__init__.py
mypackage/module1.py
mypackage/module2.py

I don't want to have to maintain multiples .pyd, so I can put my interface (.i) files all under the same project in VS2010.

Edit: So far, the only way I have been able to make it work is by duplicating my 'pyd' file into two new files: _module1.pyd and _module2.pyd. But I don't like this solution, because I need to duplicate the 'pyd' file of 30 Mo unnecessarily. I would prefer the modules to link to one '_package.pyd' file.

What is the best way of doing that?

2
it's not very clear, you cannot have different 'modules' in a .pyd, because python loader loads del dll/so and calls init<modulename> function, perhaps you can use internally a namespace and register different stuff like mymodule.stuff1 mymodule.stuff2, this should have similar namespace of different modules. I'm not sure you can cheat and call multiple Py_InitModulesherpya

2 Answers

0
votes

Finally, the easiest way and "correct" way of doing that is by creating multiple projects calling only some exposed parts of the big monolithic project. The smaller projects will be parsed by SWIG to create the Python modules. Works like a charm.

-1
votes

I just have the same problem my solution use python ctypes module

spclient_python.pyd is a pydll with two init functions the official one initspclient_python as the dll name and another module called example with initexample function. Every module in python has a corresponding initXXXXXXX function in the dll.

import ctypes
testlib = ctypes.PyDLL("spclient_python.pyd") #this will call initspclient_python 
testlib.initexample() # here I am manually calling initexample function inside spclient_python.pyd
import sys
print (sys.modules.keys()) # this will display all the modules imported...spclient_python and example will show
import spclient_python # this is for me to use the module in the code below
import example
test1=spclient_python.test1()
test2=example.test2()