问题
Running an iterative loop for a geometric progression for a time trial, using the Cython interface.
Get an error on compile (shift-enter): CompileError: command 'gcc' failed with exit status 1
%load_ext Cython
%%cython
def geo_prog_cython(double alpha, int n):
cdef double current = 1.0
cdef double sum = current
cdef int i
for i in range(n):
current = current * alpha
sum = sum + current
return sum
The error:
//anaconda/lib/python3.5/distutils/command/build_ext.py in build_extension(self, ext)
530 debug=self.debug,
531 extra_postargs=extra_args,
--> 532 depends=ext.depends)
533
534 # XXX outdated variable, kept here in case third-part code
回答1:
I know this question is quite old but I thought this may help some others.
I ran into this issue on Windows for an old Py2.7 project.
If on Windows, and using Py2.7 check that you have the MS Visual Studio C++ compiler for Python installed (download link). Not sure what changes are necessary for Py3.
For your anaconda environment, locate the Lib\distutils
directory and create a distutils.cfg
file (if not already there, otherwise just modify the current file as necessary).
You want the build config to look like below.
[build]
compiler=msvc
If on linux, make sure you have the necessary devel
packages available, e.g.
Ubuntu: apt-get install python-devel
回答2:
I was able to reproduce this without error using Anaconda3:
%load_ext Cython
%%cython -a
def geo_prog_cython(double alpha, int n):
cdef double current = 1.0
cdef double sum = current
cdef int i
for i in range(n):
current = current * alpha
sum = sum + current
return sum
Example:
geo_prog_cython(0.5, 5)
1.96875
The code seems fine. It must be an issue with your set up.
来源:https://stackoverflow.com/questions/35656604/running-cython-in-jupyter-ipython