11

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
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
rrg
  • 655
  • 2
  • 6
  • 24
  • There is no error visible. Paste the whole error report. – hruske Feb 26 '16 at 16:48
  • DistutilsExecError //anaconda/lib/python3.5/distutils/spawn.py in _spawn_posix(cmd, search_path, verbose, dry_run) 158 "command %r failed with exit status %d" --> 159 % (cmd, exit_status)) 160 elif os.WIFSTOPPED(status): DistutilsExecError: command 'gcc' failed with exit status 1 – rrg Feb 26 '16 at 17:46
  • 2
    Can you please paste the *whole* traceback,. not just the error. – hruske Feb 26 '16 at 18:24
  • have you tried using cython outside of jupyter? I think this may just be an issue with your cython configuration – Tadhg McDonald-Jensen Oct 27 '16 at 21:18
  • 1
    Any hint as to what is the _right_ Cython setup? – rrg Oct 28 '16 at 08:00

2 Answers2

6

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.

Kamil Sindi
  • 21,782
  • 19
  • 96
  • 120
6

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

ConnectedSystems
  • 892
  • 10
  • 19