5

I wrote a very simple function to properly restart ipython console with elpy when evaluating the buffer or region:

(defun my-restart-python-console ()
  "Restart python console before evaluate buffer or region to avoid various uncanny conflicts, like not reloding modules even when they are changed"
  (interactive)
  (kill-process "Python")
  (kill-buffer "*Python*")
  (elpy-shell-send-region-or-buffer))

Itt works but always ask confirmation, because "*Python*" has a running process, however I kill the process before killing the buffer.

The fun part: If I evaluate the function body line by line it works...

  • 1
    Well, this function is very useful. When I edit two or more python buffers then I need restart the python interpreter. Do you want to open an issue on elpy's repository? Sorry for write here, I can't write comments yet. I set my key binding <code>(global-set-key (kbd "C-c C-x C-c") 'my-restart-python-console)</code> – cactus Jul 9 '16 at 5:10
  • @cactus good idea I will even write a commit, alter I make the code a bit elegant. Namely it should wait until the process is properly killed. Now I have some very intensive work but it will be on my TODO list. Thanks – atevm Jul 9 '16 at 14:48
4

Well I figured it out: kill-process kills the process as a side effect, so it returns before that is really killed. So a function like this works:

(defun my-restart-python-console ()
  "Restart python console before evaluate buffer or region to avoid various uncanny conflicts, like not reloding modules even when they are changed"
  (interactive)
  (kill-process "Python")
  (sleep-for 0.05)
  (kill-buffer "*Python*")
  (elpy-shell-send-region-or-buffer))
2

If you're using ipython, why not just use the baked-in %reset magic command? You can also use:

%load_ext autoreload
%autoreload

To automatically reload imported modules on change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.