Easiest way to access C module in Python

Gisle Vanem gisle.vanem at gmail.com
Tue Nov 7 15:08:55 EST 2017


Lele Gaifax wrote:

> On my PC, I get the following, using the "-v" option to verbosely see the
> imported modules:
> 
> $ $ python -v
> # installing zipimport hook
> import zipimport # builtin
> # installed zipimport hook
> ...
>>>> import life
> dlopen("./life.so", 2);
> import life # dynamically loaded from life.so
>>>> dir(life)
> ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__test__', 'life']
> 
> Can you try to import the "life" module and print its "dir()" to see the
> symbols it exposes?

 From inside python 2.7:
   Python 2.7.13rc1 (v2.7.13rc1:4d6fd49eeb14, Dec  3 2016, 21:49:42) [MSC v.1500 32 bit (Intel)] on win32
   Type "help", "copyright", "credits" or "license" for more information.
   >>> import life
   >>> dir(life)
   ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__test__']
   >>>

works fine. But as I wrote, doing a:
   python -vc "import life; print(life.life())"

from the cmd-line doesn't work:
   ...
   import life # dynamically loaded from life.pyd
   Traceback (most recent call last):
     File "<string>", line 1, in <module>
   AttributeError: 'module' object has no attribute 'life'
   ...

Inspection life.pyd shows no problems. It do export a "initlife"
function.

-- 
--gv



More information about the Python-list mailing list