Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Cython in Jupyter iPython

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
like image 982
rrg Avatar asked Feb 26 '16 16:02

rrg


People also ask

How do I run a Jupyter Notebook in IPython?

To launch a Jupyter notebook, open your terminal and navigate to the directory where you would like to save your notebook. Then type the command jupyter notebook and the program will instantiate a local server at localhost:8888 (or another specified port).

Does Jupyter use IPython?

The IPython kernel is maintained by the Jupyter team, as a result of the evolution of the project. However, you can also run many other languages, such as Scala, JavaScript, Haskell, Ruby, and more in the Jupyter Notebook Application.

How do I run a program in IPython?

You can use run command in the input prompt to run a Python script. The run command is actually line magic command and should actually be written as %run. However, the %automagic mode is always on by default, so you can omit this.

Is Jupyter Notebook same as IPython?

The IPython Notebook is now known as the Jupyter Notebook. It is an interactive computational environment, in which you can combine code execution, rich text, mathematics, plots and rich media. For more details on the Jupyter Notebook, please see the Jupyter website.


2 Answers

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

like image 71
ConnectedSystems Avatar answered Oct 23 '22 11:10

ConnectedSystems


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.

like image 31
Kamil Sindi Avatar answered Oct 23 '22 09:10

Kamil Sindi