Re: Python to Cython conversion
2012-05-01 01:00:04 GMT
Hello,
I installed MS Visual Studio 2008 Express Edition from this link: http://www.microsoft.com/en-us/download/details.aspx?id=14597 and I'm no longer seeing the problem I was seeing before. Thanks Chris for pointing that out.
Now, finally getting to my actual Python-to-Cython conversion :), below is the Cython .pyx file I've started writing. Since this file involves usage of numpy, I'm using this link as a guide: http://docs.cython.org/src/tutorial/numpy.html
import numpy as np
# "cimport" is used to import special compile-time information
# about the numpy module (this is stored in a file numpy.pxd which is
# currently part of the Cython distribution).
cimport numpy as np
# We now need to fix a datatype for our arrays. I've used the variable
# DTYPE for this, which is assigned to the usual NumPy runtime
# type info object.
DTYPE = np.float
# "ctypedef" assigns a corresponding compile-time type to DTYPE_t. For
# every type in the numpy module there's a corresponding compile-time
# type with a _t-suffix.
ctypedef np.float_t DTYPE_t
def calculate_position(np.ndarray[DTYPE_t, ndim=1] u_A, np.ndarray[DTYPE_t, ndim=1] v_A, float t, float t_A):
assert u_A.dtype == DTYPE and v_A.dtype == DTYPE
cdef float told = t - t_A
return (u_A + v_A)*told.tolist()
However, when I run my top-level .py module, I see that the above .pyx file is compiled ok but I get the following error output:
C:\Source\src_Python_sim\PNT_Cython_001>python run_pnt.py
_tethered_particle_system.c
C:\Users\Waqas/.pyxbld\temp.win32-2.6\Release\pyrex\_tethered_particle_system.c(
237) : fatal error C1083: Cannot open include file: 'numpy/arrayobject.h': No su
ch file or directory
Traceback (most recent call last):
File "run_pnt.py", line 3, in <module>
from presynaptic_nerve_terminal_DEVS import presynaptic_nerve_terminal_DEVS
File "C:\Source\src_Python_sim\PNT_Cython_001\presynaptic_nerve_terminal_DEVS.
py", line 3, in <module>
from tethered_particle_system_DEVS import tethered_particle_system
File "C:\Source\src_Python\tethered_particle_system_DEVS.py", line 4, in <modu
le>
from tethered_particle_system import tethered_particle_system
File "C:\Source\src_Python\tethered_particle_system.py", line 5, in <module>
from _tethered_particle_system import calculate_position
File "C:\Python26\lib\site-packages\pyximport\pyximport.py", line 335, in load
_module
self.pyxbuild_dir)
File "C:\Python26\lib\site-packages\pyximport\pyximport.py", line 183, in load
_module
so_path = build_module(name, pyxfilename, pyxbuild_dir)
File "C:\Python26\lib\site-packages\pyximport\pyximport.py", line 167, in buil
d_module
reload_support=pyxargs.reload_support)
File "C:\Python26\lib\site-packages\pyximport\pyxbuild.py", line 85, in pyx_to
_dll
dist.run_commands()
File "C:\Python26\lib\distutils\dist.py", line 975, in run_commands
self.run_command(cmd)
File "C:\Python26\lib\distutils\dist.py", line 995, in run_command
cmd_obj.run()
File "C:\Python26\lib\site-packages\Cython\Distutils\build_ext.py", line 135,
in run
_build_ext.build_ext.run(self)
File "C:\Python26\lib\distutils\command\build_ext.py", line 340, in run
self.build_extensions()
File "C:\Python26\lib\site-packages\Cython\Distutils\build_ext.py", line 143,
in build_extensions
self.build_extension(ext)
File "C:\Python26\lib\distutils\command\build_ext.py", line 499, in build_exte
nsion
depends=ext.depends)
File "C:\Python26\lib\distutils\msvc9compiler.py", line 533, in compile
raise CompileError(msg)
ImportError: Building module failed: ['CompileError: command \'"C:\\Program File
s (x86)\\Microsoft Visual Studio 9.0\\VC\\BIN\\cl.exe"\' failed with exit status
2\n']
If I remove the numpy stuff from my file, then I don't see the above error output. So it is clearly something to do with how I'm using numpy. Can anyone please guide me where I'm going wrong?
Thanks,
Waqas
On Sun, Apr 8, 2012 at 7:02 AM, Waqas Muhi> There's a particular file I seem to be missing. I tried both the "importvcvarsall.bat comes with Microsoft Visual Studio (Visual C, whatever
> pyximport" as well as the "Building a Cython module using the DistUtils"
> approach mentioned here: http://docs.cython.org/src/quickstart/build.html
> In both cases, I get an error message about vcvarsall.bat:
the heck else they call it). It is used to ser of the compiler
environment when invoked.
Cython, of course, requires a compiler. distutils (and I presume
pyximport) is set up (by default) to build extensions with the same
complier and settings that Python was build with (that's one of its
core nifty features).
The Python 2.6 and 2.7 binaries distributed by python.org are built
with VS2008 (I'm pretty sure). The easiest way to get set up for that
is to install the Microsoft Visual Studio 2008 express addition (It's
free on the MS web site -- it's a bit hard to find - MS tries to get
you to use the 2010 version, but that may not work with the python
builds). I've found installing VS 2008 express then "just works" with
distutils (and thus Cython).
If you want 64 bit -- it theoretically can be done, but its takes some
hacking, and I haven't gotten it to work yet (I've given up twice
after messing around or an hour or two). But maybe I just messed
something up -- there is a page in the Cython wiki about how to do it.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker <at> noaa.gov
RSS Feed