2

How do I ensure that changed Cython code is recompiled when a module that imports it is reloaded into a Python session?

For example in utils.pyx I have something like

def func(int x):
    return x**2

while in test.py I have, for example something like

def test_cython():

    import pyximport; pyximport.install()
    import utils

    return utils.func(5)

I would like to work in IPython and have any changes to utils.pyx cause a recompilation whenever I reload test.py there, but

import test

seems to do nothing if test.py has already been imported and

reload(test) 

loads from the already compiled test.pyc, so that even if utils.pyx has been changed

test.test_cython()

uses the "old" version of utils.func.

It seems the only way to achieve this is to make an edit to test.py after each change to utils.pyx. Is there any other way to do this?


Note: I believe this is a slightly different issue from this one; in particular, this promising answer, does not solve the problem for me, even when I take the import and pyximport.install statements out of test_cython and place them at the top level of test.py.

Community
  • 1
  • 1
orome
  • 45,163
  • 57
  • 202
  • 418
  • See [reload() is broken for C extension objects](http://bugs.python.org/issue1144263) and the answer to [How to Reload a Python3 C extension module?](http://stackoverflow.com/a/8295590/4279), try [Reloading a Python extension module from IPython](http://stackoverflow.com/a/18266786/4279) using [Kernel in a separate process](http://ipython.org/ipython-doc/stable/overview.html#ipythonzmq). – jfs Dec 01 '13 at 19:57
  • @J.F.Sebastian: Those all look a bit messy. Is the short answer simply: C extensions are hard to get rid of, so it's best to just run from the command line or (re)start a new Python/IPython instance. – orome Dec 01 '13 at 20:19
  • in short: [use two process configuration](http://ipython.org/ipython-doc/stable/overview.html#ipythonzmq). It makes it trivial to restart Kernel process that allows to load updated C extension versions. For you it would look like the reload works. – jfs Dec 01 '13 at 22:04
  • @J.F.Sebastian: That makes sense, but I don't have Qt, so it's a bit less straightforward for me. Any idea what the equivalent would be on OS X (without using the "notebook" interface)? – orome Dec 01 '13 at 22:19
  • why do you think that you need Qt? Try `ipython notebook`, `ipython console` – jfs Dec 01 '13 at 22:50
  • @J.F.Sebastian: Going through the steps to use `ipython console` including how to get `reload` to work in that context sounds like it would be an answer. – orome Dec 02 '13 at 00:06

0 Answers0