【问题标题】:emacs and python updating modulesemacs 和 python 更新模块
【发布时间】:2011-10-20 00:50:06
【问题描述】:
atm 我正在使用 emacs 编写一些 python 代码,到目前为止它工作得很好,除了一个确实有点烦人的问题。
当我在自写模块中更新某些内容时,我总是重新评估缓冲区,并且 emacs 中的 python shell 中的模块不会得到更新。我总是必须结束 python 进程并重新启动它才能获得更改。我发现 emacs 将一些东西复制到 tmp 目录来执行它们,所以我想这与此有关。
也许有人遇到了同样的问题并且已经解决了,所以我们将不胜感激
【问题讨论】:
标签:
python
emacs
emacs23
python-module
【解决方案1】:
您必须在 shell 中手动重新加载模块才能使其生效。
请参阅有关 Python 重新加载功能的文档 here
我问了一个类似的问题,你可以看到here
【解决方案2】:
我找到了一个不需要 emacs 配置的更好的解决方案:
简单地做
$ ipython profile create
应该在中创建 ipython 配置文件
$HOME/.ipython/profile_default/ipython_config.py
然后把下面的内容放在里面
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 配置中有以下内容
;; ------------------
;; 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 配置,它是here
【解决方案3】:
您可以建议使用 python-mode.el 函数来获得所需的效果(至少,如果我正确理解您的请求的话)。将以下内容放入您的 Emacs 初始化文件中:
(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'):\n reload(%s)\nelse:\n import %s\n"
f f f))
(format "execfile(r'%s')\n" 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 文件,系统将提示您保存它。请注意,your previous question 的回复中提到的警告仍然适用(例如 - 如果您已经从导入的模块创建了类实例,已经实例化了其他对象等,它们将不会被重新加载)。可能会发生一般破损,因此请谨慎购买!)。
【解决方案4】:
我建议使用 here 讨论过的 Elpy。
将以下内容添加到您的 Emacs 配置文件中:
(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 缓冲区是否打开。这将有助于能够在开发的任何时候运行C-c C-x C-c,即使没有 Python 进程已经打开。另一部分是kill-buffer-query-functions,它忽略了杀死 Python 缓冲区的提示。