Here are my steps:

1- Switch to Shell C-c C-z

enter image description here

2- Click enter to accept

enter image description here

3- Eval-Buffer C-c C-c to run the script

enter image description here

I just got the following message:

sent: #!/usr/bin/env python...

Running Eval-File works fine. It's just Eval-Buffer what doesn't work.

Have I miss anything?


I've just noticed that before running the script I get the following line printed in my messages buffer:

Can't guess python-indent-offset, using defaults: 4

Which is very strange because C-h-v python-indent-offset shows me that it's already set to 4 spaces...

  • Looks like a bug to me. I'd send a bug report. This also affects the python-shell-send-region. – wvxvw Aug 22 '15 at 9:20
  • python-shell-send-region gives me the following error Wrong type of argument: keymapp, "^C^C" – RafaelGP Aug 29 '15 at 5:20
  • I've never submitted a bug to Emacs. Where do I go to do that? – RafaelGP Aug 29 '15 at 5:21
  • Well, since you've now got the answer, sending a bug report can wait :) But for future reference, you may send a bug report using: M-x report-emacs-bug. If you don't have Emacs set up to send mail, it's still useful for collecting the important info. Once it produced the message, you can send it using mail agent of your choice. – wvxvw Aug 29 '15 at 12:02

You need to call eval-buffer with a prefix argument, C-uC-c C-c

From documentation of python-shell-send-buffer

(python-shell-send-buffer &optional SEND-MAIN)

Send the entire buffer to inferior Python process. When optional argument SEND-MAIN is non-nil, allow execution of code inside blocks delimited by "if __name__== '__main__':". When called interactively SEND-MAIN defaults to nil, unless it's called with prefix argument.

| improve this answer | |
  • I have put my whole code in a main() function and call the main function at the end of the code: if __name__ == '__main__': main(). I then do C-u C-c C-c but at the minibuffer i only see the the first line of my file has been sent somewhere, in this case : Sent: import random ... and nothing more happens. What should i do more? – user12021 Aug 1 '16 at 10:28
  • @amirteymuri I think your REPL (comint) buffer is not visible, do C-c C-z to switch to it, you should see the output of your program there – Iqbal Ansari Aug 4 '16 at 5:16

I use this function for that

(defun shell-compile ()
      (shell-command (concat "python " (buffer-file-name)))
      (if (<= (* 2 (window-height)) (frame-height))
          (enlarge-window 20)
        (/ (frame-height) 2)))
    (add-hook 'python-mode-hook
              '(lambda ()
                 (define-key (kbd "C-c C-c") 'shell-compile python-mode-map)))
| improve this answer | |
  • Thank you for that. Your function works well. It prints the output in the message area, so although It doesn't solve specifically my problem, at least it allows me to run the script directly from Emacs. – RafaelGP Aug 29 '15 at 5:17
  • How should one add this function commented by @djangoliv ? – crzy Feb 25 at 4:10
  • @crzy Simply paste it in your init file. – djangoliv Feb 25 at 10:15

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.