0

I am using the following answer for How do you run Python code using Emacs.

I was wondering instead of opening python shell using C-c C-z and running C-c C-c to get results;

=> Can only the output results show up on the mini-buffer?

Opened Python shell buffer takes half of the window. I just want to see the output results in the minibuffer, or most compact buffer windows as possible.

0
1

One idea would be to use call-process or shell-command-to-string. Here is an example:

(defun python-fn (code)
  (let* ((temporary-file-directory ".")
         (tmpfile (make-temp-file "py-" nil ".py")))
    (with-temp-file tmpfile
      (insert code))
    (car (split-string (shell-command-to-string (format "python %s" tmpfile)) "\n$"))))

(python-fn "a = 150
b = 125
if b > a:
  print(\"b is greater than a\")
elif a == b:
  print(\"a and b are equal\")
else:
  print(\"a is greater than b\")")
7
  • How could I run it? M-x does not see python-fn ? C-c C-c did not make any change :-( I am getting: ‘shell-command-to-string’ is not a valid command name, but I have something called shell-command+ which seems like working but also generates stty: 'standard input': Inappropriate ioctl for device stty: 'standard input': Inappropriate ioctl for device output too
    – alper
    Nov 9 '20 at 10:51
  • 1
    A function can be made interactive (so that you can use M-x) by adding (interactive) to the first line of a defun that contains code. If the defun contains an optional doc-string, then the (interactive) statement goes below the doc-string. This example/answer is not meant to be used interactively. You can paste the example/answer to the *scratch* buffer and place your cursor just after the last closing parenthesis of each code snippet and type C-x C-e, which is bound to eval-last-sexp. I have never used shell-command+ and do not know where it is defined.
    – lawlist
    Nov 9 '20 at 14:46
  • 1
    Likewise, shell-command-to-string is not an interactive function. I am hesitant to write up an example/answer that is made to be used interactively because the python language is rather finicky in terms of code indents, and I want to avoid opening a Pandora's Box that may create a lot of work.
    – lawlist
    Nov 9 '20 at 14:52
  • elpa.gnu.org/packages/shell-command+.html ; here I can do python run.py and it runs the code and prints its output. ; emacs couldn't find shell-command-to-string in package-install section. //
    – alper
    Nov 9 '20 at 20:41
  • 1
    My example/answer relates to using shell-command-to-string, not shell-command and not shell-command+. If you are in a python file-visiting buffer and you want to run the most recent version that was saved to the hard drive, you could type M-: aka M-x eval-expression and paste (car (split-string (shell-command-to-string (format "python %s" buffer-file-name)) "\n$")) and then press the RET key. If you arelady know the file name, you could input/paste (car (split-string (shell-command-to-string (format "python %s" "/path/to/file.py")) "\n$")).
    – lawlist
    Nov 9 '20 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.