如何在运行缓冲区时强制Python Shell重新导入模块?


9

我正在使用Cc Cc将缓冲区发送到Python Shell。缓冲区的开头有一个导入。我发现,如果我修改要导入的模块,那么如果我再次使用Cc Cc运行缓冲区,它并不能反映更改(似乎Inferior Python仅执行一次导入)。

如何强制Python Shell再次导入缓冲区第一次运行中已调用的模块?

Answers:



4

这是我的工作流程。我将emacs设置为使用ipython

(setq
 python-shell-interpreter "ipython3"
 python-shell-interpreter-args "--simple-prompt --pprint")

然后在〜/ .ipython / profile_default / startup / 00-ipython_init.py中,输入以下内容:

ip = get_ipython()
ip.magic('load_ext autoreload')

然后,每当我修改并想在ipython中重新加载我的模块时,我都键入此内容。我喜欢它,因为它适用于所有模块,而且我不必担心导入依赖性。

%autoreload

1

您可以通过修改python-run并强制Python进程重新启动来完成此操作:

;; Run python and pop-up its shell.
;; Kill process to solve the reload modules problem.
(defun my-python-shell-run ()
  (interactive)
  (when (get-buffer-process "*Python*")
     (set-process-query-on-exit-flag (get-buffer-process "*Python*") nil)
     (kill-process (get-buffer-process "*Python*"))
     ;; Uncomment If you want to clean the buffer too.
     ;;(kill-buffer "*Python*")
     ;; Not so fast!
     (sleep-for 0.5))
  (run-python (python-shell-parse-command) nil nil)
  (python-shell-send-buffer)
  ;; Pop new window only if shell isnt visible
  ;; in any frame.
  (unless (get-buffer-window "*Python*" t) 
    (python-shell-switch-to-shell)))

(defun my-python-shell-run-region ()
  (interactive)
  (python-shell-send-region (region-beginning) (region-end))
  (python-shell-switch-to-shell))

(eval-after-load "python"
  '(progn
     (define-key python-mode-map (kbd "C-c C-c") 'my-python-shell-run)
     (define-key python-mode-map (kbd "C-c C-r") 'my-python-shell-run-region)
     (define-key python-mode-map (kbd "C-h f") 'python-eldoc-at-point)))

http://lgmoneda.github.io/2017/02/19/emacs-python-shell-config-eng.html


很棒的解决方案!你救了我几个小时!谢谢!
德米特里·塞梅诺夫'18 -10-22
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.