Python

Python logo.

Introduction

I joined an interesting new project about a month ago. The project uses Python as the main programming language. I have been using Python more than 20 years, but mostly for various utility type scriptings (read more about my blog posts Python Rocks! Java Man Converts to Python, and Writing Machine Learning Solutions — First Impressions). Recently, I have been using Clojure, and especially with Babashka Clojure has mostly replaced Python as my choice of programming language for scripting. There is an excellent editor/REPL integration in Clojure for practically every main stream editor. I use nowadays mostly VSCode, and for Clojure I use the excellent Calva VSCode extension to provide an editor/REPL integration for Clojure.

Is There a Good Editor/REPL Integration for Python?

Now that I’m forced to use Python, I really miss Clojure’s excellent REPL. You may say that there has always been a REPL in Python, you just launch the Python Interpreter and start writing code. But it’s not the same thing. Clojure provides excellent editor/REPL integration - you can lauch a Clojure REPL process, and connect to that process from your editor, and from the editor send the S-expressions to be evaluated in the REPL and get the results back to the editor. And that’s what I’m missing in Python.

So, I decided to spend some time searching for a good editor/REPL integration for Python. I found a couple of interesting solutions, and I decided to try them out.

bpython

If you plan to use the standard Python interpreter, then I recommend to use bpython instead. It’s not an actual editor/REPL integration solution, but at least provides a bit better syntax highlighting and autocomplete than the standard Python interpreter.

Astrapios Python REPL with iPython

Astrapios Python REPL is a VSCode extension which provides an editor/REPL integration for Python. It uses iPython as the Python REPL, so you need to install iPython to your Python environment: pip install ipython.

It’s not a perfect solution, but it’s the best I have found so far. Astrapios Python REPL starts quickly. I have configured the same hotkeys for sending the selected code / current line (alt+l), and for sending the whole file (alt+m) to be evaluated in the REPL as I have in Clojure - less cognitive burden to remember the semantically similar things in the editor with your muscle memory.

  // Clojure
  {
    "key": "alt+l",
    "command": "calva.evaluateSelection",
    "when": "calva:connected && calva:keybindingsEnabled"
  },
  {
    "key": "alt+m",
    "command": "calva.loadFile",
    "when": "calva:connected && calva:keybindingsEnabled"
  },
...
  // Python
  {   // Send the selected code or current line to REPL
      "key": "alt+l",
      "command": "pythonREPL.sendSelected",
      "when": "editorLangId == 'python'"
  },
  {   // Send all file contents to REPL
      "key": "alt+m",
      "command": "pythonREPL.sendFileContents",
      "when": "editorLangId == 'python'"
  }

Jupyter Notebook Integration

For heavier stuff, you might want to try Jupyter Notebooks in VS Code. First install Jupyter Notebook, see Installing Jupyter. Basically, you just install it using pip: pip3 install notebook. Then install the Jupyter extension for VSCode.

Start the Jupyter Notebook server from the command line:

jupyter notebook

In VSCode, create a new jupyter notebook file, eg. myjupyter.ipynb. There is a Select Kernel in the top right corner of the editor, click it. Choose Existing Jupyter Server..., and enter the url that you got in the terminal when you started the Jupyter Notebook server. The extension shows you Python3 (iPyKernel) as the kernel, choose it. Then you can start writing Python code in the notebook cells, and evaluate the cells by using the play icons.

When you launched the Jupyter Notebook, the server also opened a browser window with the Jupyter Notebook UI. You can also use that UI to create new notebooks, and open existing notebooks. The VSCode Jupyter Notebook extension provides a better editor experience, though.

Conclusions

Here are my recommendations.

  • If you want to try out some short Python one-liner, start bpython in the command line and try it out there.
  • If you are already working with the VSCode editor with some Python code base, I would install iPython in the Python virtual environment, and use Astrapios Python REPL for evaluating Python code in the editor.
  • If you are working with some data science stuff, and you want to use some scientific libraries like NumPy or Pandas, and you want to visualize the results e.g. with Matplotlib, then I would install Jupyter Notebook and the VSCode Jupyter Notebook extension.

The writer is working at a major international IT corporation building cloud infrastructures and implementing applications on top of those infrastructures.

Kari Marttila

Kari Marttila’s Home Page in LinkedIn: https://www.linkedin.com/in/karimarttila/