Skip to main content
 首页 » 编程设计

python之如何在 Emacs 中的 python-shell-send-buffer 上重新加载 python 模块

2025年05月04日11mengfanrong

当我使用 python-shell-send-buffer (C-c C-c) 时,我可以在 python shell 中看到主缓冲区的变化。

但是如果我的缓冲区有导入的模块,它们不会被重新加载。如何修复?

例如:
ma​​in.py:

from functions import foo 
print 'a' 

函数.py:

def foo(): 
    print 'bcdef' 

因此,如果我更改 foo() 并在 main.py 上运行 python-shell-send-buffer - 它会给我相同的 foo() 第一次读取

a 
bcdef   # never changed 

请您参考如下方法:

如果你想这样做 - 使用 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 将在您向其重新发送代码时重新加载模块。