如何在Emacs中使用python-shell-send-buffer重新加载python模块?

2 投票
1 回答
1123 浏览
提问于 2025-04-17 15:09

当我使用 python-shell-send-buffer(按下 C-c C-c)时,我可以在 Python shell 中看到我主文件的变化。

但是,如果我的文件中有导入的模块,它们不会被重新加载。那该怎么解决呢?

举个例子:
main.py:

from functions import foo
print 'a'

functions.py:

def foo():
    print 'bcdef'

所以,如果我修改了 foo() 函数,然后在 main.py 上运行 python-shell-send-buffer,它还是会给我第一次读取的那个 foo() 函数。

a
bcdef   # never changed

1 个回答

0

如果你想这样做,可以使用ipython。

首先,创建一个配置文件,叫“dev”(或者你喜欢的名字):

ipython profile create dev
[ProfileCreate] Generating default config file: u'/home/username/.config/ipython/profile_dev/ipython_config.py'

然后在“ipython_config.py”文件中添加以下几行:

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

接着,在Emacs的配置中为python模式设置一些变量:

(setq
   python-shell-interpreter "ipython"
   python-shell-interpreter-args "--profile=dev"
)

使用这个配置启动的IPython,当你重新发送代码时,它会自动重新加载模块。

撰写回答