emacs 和 python 更新模块

发布于 2024-11-27 16:22:54 字数 247 浏览 4 评论 0原文

atm 我正在使用 emacs 编写一些 python 代码,到目前为止它工作得很好,除了一个确实有点烦人的问题。

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

也许有人遇到了同样的问题并且已经解决了,所以我们将不胜感激

atm i'm using emacs to write some python code, so far it works quite fine except one problem that is really a bit annoying.

Always when I update something inside a self written module i reevaluate the buffer and the module in the python shell inside emacs doesn't get updated. i always have to end the python process and start it again to get the change. I figured out that emacs copies some things to a tmp dir to execute them, so i guess it has something to do with this.

Maybe someone out there had the same problem and solved it already so help would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三岁铭 2024-12-04 16:22:54

您必须在 shell 中手动重新加载模块才能使其生效。

请参阅此处有关Python重新加载函数的文档

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

You have to reload the module manually in the shell in order for it to take effect.

See the documentation here on the Python reload function

I asked a similar question which you can see here

戏舞 2024-12-04 16:22:54

我找到了一个不需要 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 配置, ,它是 此处

I found a better solution that needs no emacs config:

simply do

$ ipython profile create

that should create ipython profile in

$HOME/.ipython/profile_default/ipython_config.py  

then put the following inside

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')

then restart emacs. Now each time you save changes to file inside emacs - ipython would reload it automatically

and the following I have in my emacs config

;; ------------------
;; 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)

if you want to see my full python config, it's here

陌路黄昏 2024-12-04 16:22:54

您可以建议 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 文件(如果尚未保存)。请注意,在对 您之前的问题的回复中提到的警告 仍然适用(例如 - 如果您已经从导入的模块创建了类实例,已经实例化了其他对象等,则它们不会被重新加载)。可能会发生一般破损,所以买者自负!)。

You can advise a python-mode.el function to get the desired effect (at least, if I am understanding your request properly). Put the following in your Emacs init file:

(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))))

Now, when you're in a python program, you can select a region of code, press C-| to evaluate the region and the python program will be reloaded (or imported if it wasn't previously loaded) in the Python interpreter buffer. The ENTIRE module will be reloaded, not just the region that was selected and you will be prompted to save the python file if it hasn't already been saved. Note that the caveats that were mentioned in the replies to your previous question still apply (e.g. - If you have class instances already created from the imported module, have instantiated other objects, etc, they won't be reloaded). General breakage may occur, so caveat emptor!).

倒带 2024-12-04 16:22:54

我建议使用 Elpy,如此处< /a>.

将以下内容添加到您的 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 使用 Cc Cx Cc 运行您的代码

简而言之,此代码具有“if 子句”,用于检查 Python 缓冲区是否存在打开。这将有助于在开发的任何时候运行 Cc Cx Cc,即使没有打开任何 Python 进程也是如此。另一部分是kill-buffer-query-functions,它忽略了终止Python缓冲区的提示。

I'd recommend using Elpy as discussed here.

Add the following to your Emacs configuration file:

(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)

restart your Emacs run your code using C-c C-x C-c

In short, this code has the "if clause" for checking if Python buffer is open. This will help to be able to run C-c C-x C-c at any time of development even when there is no Python process already open. Another part is kill-buffer-query-functions which neglects the prompt for killing the Python buffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文