5

I'm using Emacs python-mode and ipython as my IDE to write the python code. Python-mode provides a nice function "py-execute-line" to run one line code, and I've bound it to key "F7".

So for the following code:

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

I can get the output printed in the ipython buffer.

In [80]: 
In [81]: Human

I'm wondering whether there's more direct way to check the value of "cell" without the print command. Something like move the cursor to the variable, press some key, and then the value is printed in the ipython output buffer.

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

I have tried function (py-execute-expression-ipython), but there is no output printed ...

Thanks in advance!

user3915365
  • 103
  • 5

3 Answers3

1

Without highlighting, I use:

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

Should be easy to adapt to choose the current selection if exists (using

guibor
  • 550
  • 5
  • 6
0

Not exactly what you want, but you can highlight a region (by pressing C-SPC to activate the mark and moving to the other side of the region) and then type M-| to run shell-command-on-region. Then simply type python RET to run Python on the region.

For example, select these two lines:

message = "Hello, Stack Overflow!"
print message

Then type M-| python RET. `Hello, Stack Overflow!" will appear as a message at the bottom of the screen. You can do this in any mode, with any shell command. See also: Shell Commands.

nanny
  • 1,098
  • 9
  • 19
  • Thanks. If I understand right, the print command is necessary in this solution. Adding print statement sometimes is annoying. In Rstudio, there is "ctr + R" to run the line or print the selected variables. The python-mode can do half of the job (run one line), and I just want the other half (check variables values without print statement). – user3915365 Aug 06 '14 at 21:01
0

Maybe consider a feature request at

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

Andreas Röhler
  • 4,804
  • 14
  • 18