Reloading a Cython module in IPython does not work

590 views
Skip to first unread message

Zak

unread,
Jun 24, 2013, 5:44:56 PM6/24/13
to cython...@googlegroups.com
Normally, I write Python code in a text editor and I test it in IPython. Every time I modify the file my_module.py, I do the following in IPython:

reload(my_module)

...and IPython picks up the changes. With Cython, the process should theoretically just have one extra step: In a separate terminal window, run 'python setup.py build_ext --inplace' and then in IPython run 'reload'. Unfortunately, this does not work. I can 'reload' all I want and it keeps using the original version of the module.

My work-around: 'exit()' from IPython and then restart IPython entirely. When exited and restarted, IPython will pick up the changes to the Cython module.

Is there a better solution?

Thank you,

Zak

Chris Barker - NOAA Federal

unread,
Jun 24, 2013, 5:49:18 PM6/24/13
to cython...@googlegroups.com
On Mon, Jun 24, 2013 at 2:44 PM, Zak <cyt...@m.allo.ws> wrote:

> My work-around: 'exit()' from IPython and then restart IPython entirely.
> When exited and restarted, IPython will pick up the changes to the Cython
> module.
>
> Is there a better solution?

nope -- Python itself can not re-load a compiled module.

Too bad, though.

-Chris


--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

Stefan Behnel

unread,
Jun 24, 2013, 11:52:49 PM6/24/13
to cython...@googlegroups.com
Zak, 24.06.2013 23:44:
In theory, PEP 3121 in Py3.

http://www.python.org/dev/peps/pep-3121/

However, the partial support in Cython doesn't currently enable reloading,
so more work needs to be done until this can be made to work.

You could use cython.inline("cython code") to compile code from a string.
It's limited to a function body, though.

You could also use pyximport with the option "reload=True" (IIRC).

Both options will simply create a new module under a new name and replace
the old one. So the reload isn't a real one, but the effect is the same.
You'll still have to restart your runtime from time to time to get rid of
the old modules, in case they hold on to any resources (at least a bit of
memory). But that's still way better than having to do it for each change.

Stefan

Reply all
Reply to author
Forward
0 new messages