I am pretty naive in cython and just beginning to explore this. So I have a very basic question. I have a python code that I am trying to cythonize. I managed to create the c source and header files from the original pyx file that has my python/cython code. But the code has the following lines:
XML_File = "some xml file name"
import xml.etree.ElementTree as ET
tree = ET.parse(XML_File)
Now when I compile the cythong C source code with my calling C program and execute the final binary I get the error saying that NameError: name 'ET' is not defined
So how do I properly import external modules/packages like "xml" or any other packages like "numpy" in my python/cython code and have it accessible from the Cython generated C code too?
My setup file is as follows: Please note that I am only dealing with one single Python code file that calls some other external python modules like "xml" "numpy" etc.
from distutils.core import setup
from distutils.extension import Extension
import sys, os
import numpy
#def find_libxml2_include():
# include_dirs = []
# for d in ['/usr/include/libxml2', '/usr/local/include/libxml2']:
# if os.path.exists(os.path.join(d, 'libxml/tree.h')):
# include_dirs.append(d)
# return include_dirs
#Determine whether to use Cython
if '--cythonize' in sys.argv:
cythonize_switch = True
del sys.argv[sys.argv.index('--cythonize')]
else:
cythonize_switch = False
#Find all includes
numpy_include = numpy.get_include()
#lxml_include = []
#for l in find_libxml2_include() + lxml.get_include():
# lxml_include.append(l)
#Set up the ext_modules for Cython or not, depending
if cythonize_switch:
from Cython.Distutils import build_ext
from Cython.Build import cythonize
ext_modules = cythonize([Extension("s2a_angle", ["s2a_angle_c.pyx"],include_dirs = [numpy_include, lxml_include])])
else:
ext_modules = [Extension("s2a_angle", ["s2a_angle_c.c"],include_dirs = [numpy_include, lxml_include])]
#Create a dictionary of arguments for setup
setup_args = {'name':'s2a_angles',
'version':'0.1.0',
'author':'Sam Hayden',
'author_email':'abc@a.gov',
'packages':[],
'py_modules' : [],
'ext_modules' : ext_modules}
#Add the build_ext command only if cythonizing
if cythonize_switch:
setup_args['cmdclass'] = {'build_ext': build_ext}
#Finally
setup(**setup_args)
I am have also tried integrating the plain cython C source with my C calling interface as follows:
gcc -I/usr/local/include/python2.7 *.c -lpython2.7 -lm -ldl -lpthread -lutil