Python Forum

Full Version: cython does not work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
cannot display Hello on cython

file: set.py
from distutils.core import setup
from Cython.Build import cythonize
import temp

compiler_directives = {
    'language_level': 2,
    'optimize.use_switch': True,
    'profile': True,
}

setup(
    packages=["XXXXXX"],
    ext_modules=cythonize("temp.pyx",
       # module_list="temp.pxd", #pyx
        #compiler_directives=compiler_directives,
    )
)
file temp.pyx
cdef class Temp1:
                
                cdef void as(self):
                                print("Hello")
Temp1.as()
as is a keyword in python. This may be the cause of the error. Can't you rename the method?
fixed as on pprr

file: set.py
from distutils.core import setup
from Cython.Build import cythonize
import temp

print("y")

compiler_directives = {
    'language_level': 2,
    'optimize.use_switch': True,
    'profile': True,
}
 
setup(
    packages=["XXXXXX"],
    ext_modules=cythonize("temp.pyx",
       # module_list="temp.pxd", #pyx
        #compiler_directives=compiler_directives,
    )
)
file temp.pyx
from time import *
#cimport ctemp

cdef class Temp1:
                
                cdef void pprr(self):
                                print("rr")

cdef Temp1 p1
p1=Temp1()
p1.pprr()
print("Hello World")
after starting set.py the program outputs the following---------------------------------------------------------------
Output:
=========== RESTART: I:\Питон\ПРОГРАМЫ PYTHON\Новая папка\setup.py =========== Hello World y Compiling temp.pyx because it changed. [1/1] Cythonizing temp.pyx >>>
It is not clear why does not display print("rr") --------------------------------------------------------
Normally you don't import the module in 'setup.py'. Removing the 'import temp', here is my output in a linux terminal
Output:
λ python setup.py build_ext --inplace Compiling temp.pyx because it changed. [1/1] Cythonizing temp.pyx running build_ext building 'temp' extension creating build creating build/temp.linux-x86_64-2.7 x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c temp.c -o build/temp.linux-x86_64-2.7/temp.o x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/temp.o -o /home/eric/Projets/Scratch/2018-12/cytruc/temp.so λ ls build setup.py temp.c temp.pyx temp.so λ python -c "import temp" rr Hello World
this command starts python temp.pyx build_ext --inplace
starting from the console results in the following
Output:
Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> temp.pyx AttributeError: module 'temp' has no attribute 'pyx'
once no more answers, I will try to make himself