Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I configure pydev to automatically compile cython files?

I'm just picking up Cython. I'm using it to build a core library in my Python project. Currently I've configured a setup.py file with distutils and am running the following command whenever I want to recompile the Cython file:

python ./setup.py build_ext --inplace

However, I often forget. I like how Eclipse automatically builds class files for Java every time I edit/save. Is it possible to configure similar behavior for PyDev, Eclipse, or some other clever way?

like image 284
Rich Avatar asked Nov 28 '11 00:11

Rich


2 Answers

For now there's no special support in PyDev to automatically compile cython files when they change... if you want you can create an external builder:

Right click project > properties > builders > new > program, then configure the program as python having as a parameter the module to run and receiving as arguments also the ${build_files} variable.

You should probably check if some changed file is a .pyx file and if it is, call the actual build command for that file -- and maybe dependencies.

like image 127
Fabio Zadrozny Avatar answered Nov 15 '22 15:11

Fabio Zadrozny


You can obtain automatically cython compiling using a "magic" sitecustomize.py in your base PYTHONPATH that calls pyximport, even if it requires some installation details (such as, under windows, your mingw location), here is an example :

import pyximport
import os
import numpy
#import cython
import Cython.Compiler.Options as Options
Options.cimport_from_pyx = True

if os.name == 'nt':
    if os.environ.has_key('CPATH'):
        os.environ['CPATH'] = os.environ['CPATH'] + numpy.get_include()
    else:
        os.environ['CPATH'] = numpy.get_include()

    # XXX: we're assuming that MinGW is installed in C:\MinGW (default)
    if os.environ.has_key('PATH'):
        os.environ['PATH'] = os.environ['PATH'] + ';C:\MinGW\bin'
    else:
        os.environ['PATH'] = 'C:\MinGW\bin'

    mingw_setup_args = { 'options': { 'build_ext': { 'compiler': 'mingw32' } } }
    pyximport.install(setup_args=mingw_setup_args)
elif os.name == 'posix':
    if os.environ.has_key('CFLAGS'):
        os.environ['CFLAGS'] = os.environ['CFLAGS'] + ' -I' + numpy.get_include()
    else:
        os.environ['CFLAGS'] = ' -I' + numpy.get_include()

    pyximport.install()

pyximport.DEBUG_IMPORT = True

As a side note, if you are under windows, please keep in mind that your cython must be slightly modified to use mingw.

You should also call your files *.pyx for this to work. Another advice : you should use the cython "pure python" syntax so that the Pydev editor will not complain.

like image 26
Luca Dall'Olio Avatar answered Nov 15 '22 15:11

Luca Dall'Olio