Using SWIG to build Python extensions -- help!

Carl Bevil carl_bevil at yahoo.com
Fri Jan 24 21:12:30 EST 2003


Well, I'm embarrassed to say it, but I think I know where my problem lies.  If 
anyone's interested, read on.  And feel free to correct me if I'm 
misunderstanding anything.  :-)

Basically, SWIG creates the wrapper file in C/C++, but the high-level module 
code lies in the .py file.  So the module in the C/C++ file gets an underscore 
prepended to its name, and you have to manually import the .py file to use it 
without the underscore.

I was able to adjust the entry in my import init table to use "_swigtest" as 
the module name, and then I could import the module and call the test function 
like I want to ("import _swigtest",  "_swigtest.function()").

In this simple test, the above hack works OK.  But it is a hack, and I'm not 
sure if it will work with more complicated examples, such as C++ classes, 
templates, etc...  I'll do some more tests soon, but even if all the tests 
come out positive I'll still have doubts.

So I'm going to look into boost.python (as some people have suggested to me 
privately).  But I'd prefer to use SWIG and be able to automate everything.  
I'm trying to convince the rest of my team that using Python will make our 
lives easier, and if I tell them they have to code wrappers (even using a tool 
that makes it easier, like boost.python) I'm not sure how they'll react.

As always, if anyone has any suggestions, feel free to contact me here or in 
e-mail (carl_bevil at yahoo.com).

Thanks for your patience,

Carl  (A Python newbie)

On Fri, 24 Jan 2003 12:19:59 -0800 Carl Bevil <carl_bevil at yahoo.com> wrote:

> Hello all.  I'm experimenting with using SWIG to build a Python extension.  
I 
> have a really simple C++ file that I'd like to extend Python with as a test.
> 
> I'm building the interpreter as a library that I statically link into my 
main 
> application.  Dynamic linking is not an option for my target platform, so 
I've 
> downloaded the Python source code.  I'm using Python 2.1.3.
> 
> I create two files, swigtest.cpp and swigtest.h.  swigtest.h has a function 
> prototype, and swigtest.cpp has the function body.
> 
> Then I run SWIG:
> 
> swig -python -c++ -module swigtest swigtest.h
> 
> This builds two files: swigtest.py and swigtest_wrap.cxx.  I rename the 
latter 
> to swigtest.cpp, as the compiler I'm using (Metrowerks) doesn't like .cxx 
files.
> 
> Then I compile and link swigtest_wrap.cpp and swigtest.cpp into the python 
> interpreter, creating a python21_d.lib file (I'm on Windows).
> 
> Then I build my main application (a C++ app), and link it statically with 
the 
> python interpreter library that I just built.
> 
> When I run, I execute the following statement:
> 
> PyRun_SimpleString("import swigtest");
> 
> ... and I get an error that there is no module named swigtest.
> 
> What am I doing wrong here?  And do I need to do anything with the 
swigtest.py 
> file that SWIG creates? I've read the chapter in O'Reilly's Programming 
Python 
> on extending Python, but it makes no mention of SWIG generating a .py file.  
> And all of its examples are dynamically linked.
> 
> Thanks!
> 
> Carl
> 




More information about the Python-list mailing list