简体   繁体   中英

Why won't my module work even though I can import it?

I am following a tutorial from: https://en.wikibooks.org/wiki/Python_Programming/Extending_with_C I am referring to the section entitled "Using Swig". I create the files in his location:

~/Desktop/TEST2/helloMODULEtest

I first create a file called hellomodule.c with this code init:

/*hellomodule.c*/

    #include <stdio.h>

    void say_hello(const char* name) {
            printf("Hello %s!\n", name);
    }

I then create the file hello.i in the same location:

/*hello.i*/

%module hello
extern void say_hello(const char* name);

I have swig and python-dev installed on the Pi. So typing these three lines in the terminal:

$ swig -python hello.i
$ gcc -fpic -c hellomodule.c hello_wrap.c -I/usr/include/python2.7/
$ gcc -shared hellomodule.o hello_wrap.o -o _hello.so -lpython2.7

gives me these files:

  • hellomodule.o
  • hello.py
  • _hello.so
  • hello_wrap.c
  • hello_wrap.o

I then sudo copy hello.py and _hello.so to my python2.7 lib file:

$ sudo cp ~/Desktop/TEST2/helloMODULEtest/hello.py /usr/lib/python2.7
$ sudo cp ~/Desktop/TEST2/helloMODULEtest/_hello.so /usr/lib/python2.7

After these steps I can the Python 2.7.9 shell. In the shell I type

 >>> import hello

and receive no warnings or errors, an indication that hello.py has been imported. But, when I type

>>> hello.say_hello("World")

The module appears to return nothing and goes to the next line. This is the "dialog" for the import and call to hello with the next entry line I have:

>>> import hello
>>> hello.say_hello("World")
>>>

I would like to see this:

>>> import hello
>>> hello.say_hello("World")
Hello World!
>>>

So, my question is why is nothing being returned from my hello.py module?

Update your hello.i file to this:

/*hello.i*/

%module hello
%{
void say_hello(const char* name);
%}

void say_hello(const char* name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM