emacs和python更新模块

emacs and python updating modules

atm我正在使用emacs编写一些python代码,到目前为止,除了一个确实有点烦人的问题之外,它的工作情况还不错。

每当我更新自写模块中的内容时,我都会重新评估缓冲区,而emacs中的python shell中的模块不会更新。 我总是必须结束python进程并再次启动它以获取更改。 我发现emacs将一些东西复制到tmp目录中以执行它们,因此我想这与此有关。

也许有人在那里遇到了同样的问题并已经解决了,所以请多多帮助


您必须在外壳中手动重新加载模块才能生效。

请参阅此处有关Python重新加载功能的文档

我问了一个类似的问题,你可以在这里看到


我找到了一个不需要emacs配置的更好的解决方案:

简单地做

1
$ ipython profile create

应该在中创建ipython配置文件

1
$HOME/.ipython/profile_default/ipython_config.py

然后放入以下内容

1
2
3
4
5
6
7
8
9
c = get_config()
c.TerminalInteractiveShell.editor = 'emacsclient'
c.InteractiveShellApp.extensions = [
     'autoreload'
]

c.InteractiveShellApp.exec_lines = []
c.InteractiveShellApp.exec_lines.append('%load_ext autoreload')
c.InteractiveShellApp.exec_lines.append('%autoreload 2')

然后重新启动emacs。现在,每次将更改保存到emacs中的文件时-ipython都会自动重新加载它

我的emacs配置中有以下内容

1
2
3
4
5
6
7
8
9
10
11
12
;; ------------------
;; misc python config
;; ------------------
(company-mode -1)
(elpy-enable)
(elpy-use-ipython"ipython")
(setq python-shell-interpreter"ipython" python-shell-interpreter-args"--simple-prompt --pprint")
(setq python-check-command"flake8")
(setq elpy-rpc-backend"jedi")
(setq elpy-rpc-python-command"python")
; https://github.com/gregsexton/ob-ipython/issues/28
(setq python-shell-completion-native-enable nil)

如果您想查看我的完整python配置,请在这里


我建议按照此处所述使用Elpy。

将以下内容添加到您的Emacs配置文件中:

1
2
3
4
5
6
7
8
(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)
  (if (get-buffer"*Python*")
      (let ((kill-buffer-query-functions nil)) (kill-buffer"*Python*")))
  (elpy-shell-send-region-or-buffer))

(global-set-key (kbd"C-c C-x C-c") 'my-restart-python-console)

重新启动Emacs,使用C-c C-x C-c运行代码

简而言之,此代码具有" if子句",用于检查Python缓冲区是否已打开。这将有助于即使在没有打开任何Python进程的情况下,也可以在开发的任何时间运行C-c C-x C-c。另一部分是kill-buffer-query-functions,它忽略了杀死Python缓冲区的提示。


您可以建议使用python-mode.el函数以获得所需的效果(至少,如果我正确理解了您的要求)。将以下内容放入您的Emacs初始化文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(defun py-reload-file (buf)
 "Reload buffer's file in Python interpreter."
  (let ((file (buffer-file-name buf)))
    (if file
        (progn
          ;; Maybe save some buffers
          (save-some-buffers (not py-ask-about-save) nil)
          (let ((reload-cmd
                 (if (string-match"\\\\.py$" file)
                     (let ((f (file-name-sans-extension
                               (file-name-nondirectory file))))
                       (format"if globals().has_key('%s'):\
    reload(%s)\
else:\
    import %s\
"

                               f f f))
                   (format"execfile(r'%s')\
"
file))))
            (save-excursion
              (set-buffer (get-buffer-create
                           (generate-new-buffer-name" *Python Command*")))
              (insert reload-cmd)
              (py-execute-base (point-min) (point-max))))))))

(defadvice py-execute-region
  (around reload-in-shell activate)
 "After execution, reload in Python interpreter."
  (save-window-excursion
    (let ((buf (current-buffer)))
      ad-do-it
      (py-reload-file buf))))

现在,当您在python程序中时,可以选择一个代码区域,按C- | 评估该区域,然后python程序将在python解释器中重新加载(或导入,如果之前未加载过)。缓冲。整个模块将被重新加载,而不仅仅是选定的区域,如果尚未保存python文件,系统将提示您保存它。请注意,对上一个问题的答复中提到的警告仍然适用(例如-如果您已经从导入的模块中创建了类实例,已经实例化了其他对象,等等,则不会重新加载它们)。可能会发生一般性破损,因此请告诫!)。