jorgenschaefer / elpy

Emacs Python Development Environment
GNU General Public License v3.0
1.89k stars 259 forks source link

restart python to make code changes take effect #1784

Open q1999 opened 4 years ago

q1999 commented 4 years ago

Summary

To make code changes take effect, I need to restart python every time. For which there doesn't seem to be a default key combination, but that is beside the point except for that I think it points to an action that should not be a much used part of a workflow. Am I doing something wrong or is this "works as intended"?

Steps to reproduce

1) open file a.py that imports file b.py from same directory (a calling function in b, for instance to print something). 2) Activate Virtual Env, or not, makes no difference 3) C-c C-c to send buffer to python (args being either -i or -i -B) 4) change some existing code in b.py (for instance some string to be printed) 5) C-c C-c will print previous string from b.py, not the new string. 6) only after restarting python will C-c C-c print the new string.

My configuration

GNU Emacs 25.1.1 (x86_64-w64-mingw32) of 2016-09-17

OS

Windows 7 Service Pack 1

Result of (elpy-config)

# Paste it here

Elpy Configuration

Emacs.............: 25.1.1
Elpy..............: 1.33.0
Virtualenv........: None
Interactive Python: python 3.8.2 (c:/Users/SENNAC/AppData/Local/Programs/Python/Python38-32/python.exe)
RPC virtualenv....: rpc-venv (c:/Users/SENNAC/EmacsHome/.emacs.d/elpy/rpc-venv)
 Python...........: c:/Windows/py.exe 3.8.2 (c:/Windows/py.exe)
 Jedi.............: 0.16.0 (0.17.0 available)
 Rope.............: 0.16.0
 Autopep8.........: 1.5.1 (1.5.2 available)
 Yapf.............: 0.29.0 (0.30.0 available)
 Black............: 19.10b0
Syntax checker....: flake8.exe (c:/Users/SENNAC/AppData/Local/Programs/Python/Python38-32/Scripts/flake8.exe)

Elpy configuration in my init.el


# Paste it here
3 matches in 2 lines for "elpy" in buffer: .emacs

     91:    '(package-selected-packages
                (quote 
                 (pydoc-info elpy elpygen helm-pydoc netherlands-holidays py-isort tj3-mode vlf ac-slime slime-company async auto-complete avy biblio biblio-core cider clojure-mode csharp-mode dash deferred emmet-mode epl f flx flycheck ghub git-commit haml-mode htmlize hydra ivy js2-mode json-reformat json-snatcher key-chord let-alist macrostep magit-popup parsebib pdf-tools pkg-info popup projectile queue request s seq shut-up simple-httpd skewer-mode spinner tablist with-editor yasnippet rainbow-delimiters anaconda-mode websocket visual-regexp undo-tree smartparens sass-mode request-deferred pyimport paredit org-ref org omnisharp move-text memory-usage magit log4e ledger-mode json-mode js2-refactor immortal-scratch gulp-task-runner gnuplot-mode gnuplot git-wip-timemachine git-timemachine ggtags flycheck-elm flx-ido expand-region elm-yasnippets elm-mode elisp-lint company common-lisp-snippets clojure-cheatsheet charmap ace-window ac-js2 ac-ispell ac-cider 2048-game)))
    304:   (add-hook 'python-mode-hook     'elpy-mode)```
galaunay commented 4 years ago

If you are using IPython, you can use the %autoreload magic. But as the IPython documentation say:

Reloading Python modules in a reliable way is in general difficult, and unexpected things may occur.

What I generally do is restarting the shell (C-c C-k for elpy-shell-kill then C-c C-c). If you do that very often, it may worth having a keybinding for it:

(defun elpy-shell-restart-and-send-region-or-buffer ()
  "Send the region of buffer after restarting the shell."
  (interactive)
  (elpy-shell-kill t)
  (elpy-shell-send-region-or-buffer))
(define-key elpy-mode-map (kbd "C-c C-a")
  'elpy-shell-restart-and-send-region-or-buffer)

Or using importlib.autoreload, when my script takes a bit of time running.

q1999 commented 4 years ago

If you are using IPython, you can use the %autoreload magic. But as the IPython documentation say:

Reloading Python modules in a reliable way is in general difficult, and unexpected things may occur.

What I generally do is restarting the shell (C-c C-k for elpy-shell-kill then C-c C-c). If you do that very often, it may worth having a keybinding for it:

(defun elpy-shell-restart-and-send-region-or-buffer ()
  "Send the region of buffer after restarting the shell."
  (interactive)
  (elpy-shell-kill t)
  (elpy-shell-send-region-or-buffer))
(define-key elpy-mode-map (kbd "C-c C-a")
  'elpy-shell-restart-and-send-region-or-buffer)

Or using importlib.autoreload, when my script takes a bit of time running.

thanks galaunay for your answer and input. Might not even rebinding C-c C-c to your restart function be an option, instead of binding it to C-c C-a? Not sure. C-c C-c is a key combination used by both python mode and elpy mode. Anyway, I will give your code a try.

galaunay commented 4 years ago

If you don't use the default C-c C-c, there is nothing preventing you from rebinding elpy-shell-restart-and-send-region-or-buffer to C-c C-c.