【问题标题】:ipython not reloading modulesipython没有重新加载模块
【发布时间】:2016-05-20 02:01:17
【问题描述】:

我正在使用以下命令在 emacs shell 中运行 IPython:

;; Set IPython interpreter in my init.el
(defvar python-shell-interpreter "ipython")
(defvar python-shell-interpreter-args "-i")

然后:

  1. 使用M-x run-python 启动 IPython
  2. 在 IPython 中使用 %run myprog.py 运行程序。 myprog.py 导入一个名为 mymodule 的模块。

我对@9​​87654325@ 进行了更改,但是当我再次运行%run myprog.py 时,它运行的是原始mymodule,而不是更改后的代码。

FWIW,我在带有 Anaconda 和 Python 3.5 的 Windows 10 上使用 emacs 24.5 prelude。

【问题讨论】:

  • 从 Emacs 端看不到缓存。假设编辑了错误的文件。

标签: python shell ipython reload


【解决方案1】:

原来是IPython's %run command does not reload modules。

我目前的解决方法是:

  1. 将下面的代码添加到~/.ipython/profile_default/ipython_config.py
  2. 使用$run myprog.py args

.

# this code in `~/.ipython/profile_default/ipython_config.py`
# get_config() is injected into the global namespace whilst
# config files are loaded
c = get_config()

# Autoreload modules
c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']

我没有意识到 %run 不会重新加载模块,因为我习惯使用 Spyder 的 runfile 命令,它确实如此。 %run 没有,这太疯狂了,我想在某个时候提交一个补丁来修复它。

在 Windows 上,必须设置 HOME 环境变量,以便 emacs 中的 run-python 命令可以读取 IPython 配置文件。如果没有设置 HOME,您可以将其添加到您的 init.el:

(add-hook 'inferior-python-mode-hook (lambda ()
                                       (progn
                                         (python-shell-send-string-no-output "%load_ext autoreload")
                                         (python-shell-send-string-no-output "%autoreload 2"))))

【讨论】:

    猜你喜欢
    • 2018-12-17
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 2017-06-19
    • 2016-05-04
    • 2014-05-22
    • 2020-01-20
    相关资源
    最近更新 更多