How to get up and running with Python in Emacs

I did some programming in Python many years ago (around 2001), but haven’t touched it since, though I always thought I would come back to it at some point. Today, I finally got around to installing and testing it a little.

The python installation on Windows 10 was impressively easy (get it at I had it up an running in the REPL (or the IDLE, as it is called in the case of Python) almost immediately. Writing a new script is as easy as opening a new file using the standard top menu, and pressing F5 to run it in the REPL. Wonderful. Now to get it working in Emacs.

A little bit of searching indicates there is lots of relatively advanced information available, but all I wanted at this point was a quick and simple way to get a REPL with python running in Emacs. How to do this was not obvious, but in the end this too turned out to be easy.

Running python:

  • Open a Python shell in Emacs: C-c C-z
    Note: When I installed Python, I made sure to add python to the environment variables. I’m assuming this is why this worked so easily. Apparently, support for python mode (python.el) is included from emacs24 and onwards, and once pythons was installed on my system, nothing more was needed for Emacs to find and run it.

  • Run a Python script: C-c C-c
  • This assumes the file is open in Emacs, and that a python shell is running.

  • Run a selected region of a loaded script: C-c C-r

    …And that’s basically it. You should now be able to open a file. write Python code in it, and compile it in the REPL, as well as enter Python commands directly in the REPL.