【问题标题】:Execute the expression and print its value in Emacs python mode在 Emacs python 模式下执行表达式并打印它的值
【发布时间】:2014-09-29 18:41:02
【问题描述】:

我使用 Emacs python-mode 和 ipython 作为我的 IDE 来编写 python 代码。 Python-mode 提供了一个很好的函数“py-execute-line”来运行一行代码,我已经将它绑定到键“F7”。

所以对于下面的代码:

cell = "Human" #  Move the cursor to this line and run it by pressing F7
print cell     #  Move the cursor to this line and run it by pressing F7

我可以在 ipython 缓冲区中打印输出。

In [80]: 
In [81]: Human

我想知道是否有更直接的方法可以在不使用打印命令的情况下检查“单元格”的值。像将光标移动到变量上,按下某个键,然后将值打印在 ipython 输出缓冲区中。

cell = "Human" #  Move the cursor to this line and run it by pressing F7
cell = "Human" #  Move the cursor to "cell" and print its value by pressing ?? key or function

我试过函数(py-execute-expression-ipython),但是没有输出输出...

提前致谢!

【问题讨论】:

    标签: emacs ipython python-mode


    【解决方案1】:

    没有突出显示,我使用:

    (defun python-eval-expression ()
      (interactive)
      (let ((py-command (read-string "Command: ")))
        (save-excursion
          (setq elpy-shell-echo-output t)
          (setq elpy-shell-echo-input t)
          (elpy-shell--with-maybe-echo (python-shell-send-string py-command)))))
    

    如果存在,应该很容易适应选择当前选择(使用

    【讨论】:

      【解决方案2】:

      也许考虑在

      提出功能请求

      https://bugs.launchpad.net/python-mode

      【讨论】:

      • 谢谢,我稍后再做。
      【解决方案3】:

      不完全是您想要的,但您可以突出显示一个区域(通过按 C-SPC 激活标记并移动到该区域的另一侧),然后键入 M-| 以运行 shell-command-on-region。然后只需键入 python RET 即可在该区域上运行 Python。

      例如,选择这两行:

      message = "Hello, Stack Overflow!"
      print message
      

      然后输入M-| python RET。 “你好,堆栈溢出!”将在屏幕底部显示为一条消息。您可以在任何模式下使用任何 shell 命令执行此操作。另请参阅:Shell Commands

      【讨论】:

      • 谢谢。如果我理解正确,则此解决方案中需要 print 命令。添加打印语句有时很烦人。在 Rstudio 中,有“ctr + R”来运行该行或打印选定的变量。 python 模式可以完成一半的工作(运行一行),而我只想要另一半(检查变量值而不使用 print 语句)。
      猜你喜欢
      • 2019-03-29
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 2022-01-12
      • 2021-04-26
      相关资源
      最近更新 更多