5

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?

UPDATE:

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...

4
  • Looks like a bug to me. I'd send a bug report. This also affects the python-shell-send-region.
    – wvxvw
    Commented Aug 22, 2015 at 9:20
  • python-shell-send-region gives me the following error Wrong type of argument: keymapp, "^C^C"
    – RafaelGP
    Commented Aug 29, 2015 at 5:20
  • I've never submitted a bug to Emacs. Where do I go to do that?
    – RafaelGP
    Commented Aug 29, 2015 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
    Commented Aug 29, 2015 at 12:02

2 Answers 2

7

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.

2
  • 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
    Commented Aug 1, 2016 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 Commented Aug 4, 2016 at 5:16
1

I use this function for that

(defun shell-compile ()
      (interactive)
      (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)))
5
  • 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
    Commented Aug 29, 2015 at 5:17
  • How should one add this function commented by @djangoliv ?
    – crzy
    Commented Feb 25, 2020 at 4:10
  • @crzy Simply paste it in your init file.
    – djangoliv
    Commented Feb 25, 2020 at 10:15
  • The first bit is very nice! The second bit doesn't seem to work though - I get Wrong type argument: keymapp, "^C^C". How can I bind this function to C-c C-c?
    – AstroFloyd
    Commented Apr 5 at 7:22
  • OK, I ended up replacing the second part ((add-hook ...) with (define-key python-mode-map (kbd "C-c C-c") 'shell-compile), and then it works fine! Thanks! (Actually, I replace python-mode-map with elpy-mode-map w.r.t. the above, since I use elpy). I still need to figure out how to tune the size (number of lines) of the output frame.
    – AstroFloyd
    Commented Apr 5 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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