Easiest way to access C module in Python

Gisle Vanem gisle.vanem at gmail.com
Tue Nov 7 13:38:07 EST 2017


Lele Gaifax wrote:

> $ python setup.py build_ext --inplace
> Compiling life.pyx because it changed.
> [1/1] Cythonizing life.pyx
> running build_ext
> building 'life' extension
> creating build
> creating build/temp.linux-x86_64-3.6
> x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fdebug-prefix-map=/build/python3.6-5reRaQ/python3.6-3.6.3=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/tmp/ct/include -I/usr/include/python3.6m -c life.c -o build/temp.linux-x86_64-3.6/life.o
> x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fdebug-prefix-map=/build/python3.6-5reRaQ/python3.6-3.6.3=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/tmp/ct/include -I/usr/include/python3.6m -c fred.c -o build/temp.linux-x86_64-3.6/fred.o
> x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -specs=/usr/share/dpkg/no-pie-link.specs -Wl,-z,relro -specs=/usr/share/dpkg/no-pie-link.specs -Wl,-z,relro -g -fdebug-prefix-map=/build/python3.6-5reRaQ/python3.6-3.6.3=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.6/life.o build/temp.linux-x86_64-3.6/fred.o -o /tmp/ct/life.cpython-36m-x86_64-linux-gnu.so
> $ python -c "import life; print(life.life())"
> 42

I tried your example on Python 2.7 (Win-10 / MSVC), but failed:

python.exe setup.py build_ext --inplace
running build_ext
building 'life' extension
creating build
creating build\temp.win32-2.7
creating build\temp.win32-2.7\Release
f:\gv\VC_2017\VC\Tools\MSVC\14.11.25503\bin\HostX86\x86\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG
-If:\ProgramFiler\Python27\include -If:\ProgramFiler\Python27\PC /Tclife.c /Fobuild\temp.win32-2.7\Release\life.obj
life.c
f:\gv\VC_2017\VC\Tools\MSVC\14.11.25503\bin\HostX86\x86\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG
-If:\ProgramFiler\Python27\include -If:\ProgramFiler\Python27\PC /Tcfred.c /Fobuild\temp.win32-2.7\Release\fred.obj
fred.c
f:\gv\VC_2017\VC\Tools\MSVC\14.11.25503\bin\HostX86\x86\link.exe /DLL /nologo /INCREMENTAL:NO
/LIBPATH:f:\ProgramFiler\Python27\libs /LIBPATH:f:\ProgramFiler\Python27\PCbuild
/EXPORT:initlife build\temp.win32-2.7\Release\life.obj build\temp.win32-2.7\Release\fred.obj
/OUT:F:\ProgramFiler\Python27\test\Cython-test2\life.pyd /IMPLIB:build\temp.win32-2.7\Release\life.lib
/MANIFESTFILE:build\temp.win32-2.7\Release\life.pyd.manifest
    Creating library build\temp.win32-2.7\Release\life.lib and object build\temp.win32-2.7\Release\life.exp

python.exe -c "import life; print(life.life())"
Traceback (most recent call last):
   File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'life'


Can you give a hint?

-- 
--gv



More information about the Python-list mailing list