How do you run Python code using Emacs? How do you run Python code using Emacs? python python

How do you run Python code using Emacs?


If you are using emacs24 this should be the default (in emacs23 you need python.el, not python-mode.el):

In a python buffer:

  • C-c C-z : open a python shell
  • C-c C-c : run the content of the buffer in the opened python shell
  • C-c C-r : run the selected region in the python shell

default python shell is "python", if you nee to use ipython you can use this conf in your .emacs

(setq python-shell-interpreter "ipython" python-shell-interpreter-args "--colors=Linux --profile=default" python-shell-prompt-regexp "In \\[[0-9]+\\]: " python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: " python-shell-completion-setup-code "from IPython.core.completerlib import module_completion" python-shell-completion-module-string-code "';'.join(module_completion('''%s'''))\n" python-shell-completion-string-code "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")

provided that you have ipython installed in your system of course :)

ipython>=5 has a auto-complete feature which breaks the emacs sub-shell, you can fix this by changing this linepython-shell-interpreter-args "--colors=Linux --profile=default"and add --simple-prompt.

It will allow you to see ipython correctly but for some reason I did not get yet the auto-completion in emacs is not as effective as it used to be.


How do you run Python code using Emacs?

I'm running Emacs 26, vanilla dev version (self compiled from source cloned from Savannah).

(Note that in emacs docs, we usually see, for example, Ctrl-c denoted as C-c)

In Python mode (which I usually enter by using C-x C-f to "find" a (possibly new) file ending in .py), you can start a Python shell with and then execute your buffer's
if __name__ == '__main__': with:

  • C-c C-p (which executes run-python to create a shell with Inferior Python major mode, Shell-Compile minor mode)

  • C-u C-c C-c (which executes python-shell-send-buffer with a prefix argument)

We require the prefix argument to send the if __name__ == '__main__': block to the inferior Python shell.

We can see all of the Ctrl-c commands with Ctrl-c ?

C-c C-c       python-shell-send-bufferC-c C-d       python-describe-at-pointC-c C-f       python-eldoc-at-pointC-c C-j       imenuC-c C-l       python-shell-send-fileC-c C-p       run-pythonC-c C-r       python-shell-send-regionC-c C-s       python-shell-send-stringC-c C-t       Prefix CommandC-c C-v       python-checkC-c C-z       python-shell-switch-to-shellC-c <     python-indent-shift-leftC-c >     python-indent-shift-rightC-c C-t c python-skeleton-classC-c C-t d python-skeleton-defC-c C-t f python-skeleton-forC-c C-t i python-skeleton-ifC-c C-t m python-skeleton-importC-c C-t t python-skeleton-tryC-c C-t w python-skeleton-while

Inspecting the help for python-shell-send-buffer (by clicking it), we see:

python-shell-send-buffer is an interactive compiled Lisp function in‘python.el’.(python-shell-send-buffer &optional SEND-MAIN MSG)Send the entire buffer to inferior Python process.When optional argument SEND-MAIN is non-nil, allow execution ofcode inside blocks delimited by "if __name__== '__main__':".When called interactively SEND-MAIN defaults to nil, unless it’scalled with prefix argument.  When optional argument MSG isnon-nil, forces display of a user-friendly message if there’s noprocess running; defaults to t when called interactively.

According to the docs C-u is a prefix argument - and seems to be the most generic one.

A workaround that lets us avoid using the prefix argument C-u is using parentheses:

if (__name__ == '__main__'):    main()

instead of the usual:

if __name__ == '__main__':    main()

and then C-c C-c by itself executes the main function.


In my opinion, M-! and M-& are underrated. Often you just want to start the current script you are working on, no need to complicate things.

Of course, you can use M-x compile, as long as you don't have to provide interactive input. If you do have to provide interactive input, M-x shell is your friend.

If you want to run stuff with one button press, check out F3 and F4 to record a keyboard macro and replay it (the macro can also be bound to a key, e.g. F5).

In each of these cases, there is no "magic" taking place. Emacs does not know how to "compile" or "run" python scripts. You have to provide/overwrite a sensible command line invocation, like:

Compile Command: ipython <yourscriptname>.py

The python subshell is cool where a REPL development style makes sense, but at least on Windows matplotlib, tkinter and other libraries that have to deal with the Windows Message Loop tend to block/hang upon displaying GUI elements.