I have been experimenting with using SWIG to wrap C code for use in Python.I am using MSYS and MingGW on a Windows 10 platform. I have written a small test library
#include "add.h"
double add(double a, double b)
{
double c = a+b;
return c;
}
with header
#ifndef __ADD_H
#define __ADD_H
extern double add(double a, double b);
#endif
The interface file is testpackage.i
%module testpackage
%{
#include "add.h"
%}
double add(double, double);
This is then compiled
swig -python -py3 testpackage.i
gcc -c -Wall add.c add_wrap.c
gcc -shared add.o add_wrap.o -L/mingw64/lib -lpython3.9 -o _testpackage.pyd
A small python program to test this:
import testpackage
x = 2.3
y = 5.6
z = testpackage.add(x,y)
print(z)
Now this compiles without errors or warnings and runs perfectly in the MSYS terminal. (I also wrote some more complex multi-file libraries which also worked fine) The python program resides in the same directory as the generated testpackage.py and _testpackage.pyd files. However, if I try to run the python script in my Anaconda cmd or PowerShell terminals, or if I load it into Spyder and run from there. I get the error:
(base) C:\msys64\home\hoyla\swigHelloWorld>python test1.py
Traceback (most recent call last):
File "test1.py", line 1, in <module>
import testpackage
File "C:\msys64\home\hoyla\swigHelloWorld\testpackage.py", line 15, in <module>
import _testpackage
ImportError: DLL load failed while importing _testpackage: The specified module could not be found.
Now I know that the Python which ships with MinGW and is running in the MSYS terminal is 3.9.6 whereas the Anaconda Python is 3.8.11. Could this be the issue? If this is the case, how can I build my library with backwards compatibility in mind? Or am I just missing something else here?
Update: Managed to install 3.9.6 in a conda environment and still get module could not be found - so that's not the problem!
Further update: Running the Dependencies (x64) I discovered that _testpackage.pyd requires libPython3.9.dll (obvs - since I linked it!). However, outside of the MSYS terminal this can't be found as the folder was not in the PATH. I added it to the PATH, dependency walker now locates it and the error message is gone when I run from Anaconda console. However now the test script crashes with no error message at all if I import my testpackage module - either running my test py script or if I import in the interactive python - python just exits. Everything still works find in MSYS console.