I have found one of the best packages in emacs to be ESS mode which allows me to work seamlessly with R. I much prefer the setup and workflow of ESS than R Studio which I had previously done most of my R coding in. However, I spend most of my time coding in Python in jupyter notebooks. I have tried on several occasions to switch to using jupyter notebooks inside emacs, but it has never quite clicked for me and I’ve always gone back to just working in a browser. Even with the improvements brought about with JupyterLab I still find this workflow far from ideal but I have stuck with it mostly out of inertia. However, it is possible to replicate the ESS workflow for Python, and since I didn’t find any resource providing a step-by-step guide on how to do this I have written one here for anyone else that might be interested.
The first thing we need to do is to get Spacemacs to recognize and speak to our Anaconda environments. To do this you need to add conda package to your ‘dotspacemacs-configuration-layers’.
(conda :variables conda-anaconda-home "/Users/admin/opt/anaconda3/")
With the path needing to be changed to wherever your Anaconda distribution is stored. Then the python layer needs to be pointed towards the Anaconda layer. This can be done by adding the following variables in the ‘dotspacemacs-configuration-layers’.
(python :variables python-backend 'anaconda)
With this in place, the basic structure of being able to send code in a buffer to an interactive conda shell is in place.
Now we can open a python file and start setting up our workflow. The first thing we need to do is to activate a conda environment. This can be done via SPC m n a or the ‘conda-env-activate’ command. With the conda environment activated we can then open up an interactive shell viaSPC m ’ or ‘python-start-or-switch-repl’. Everything is now in place to work with python code as you would with R in ESS mode.
To send code to the interactive shell, there are several options available via the repl menu of SPC m s. I have found the most useful command to be ‘python-shell-send-region’ which can be used to send either a highlighted region or a code in between two newlines to the interactive shell. I have bound this to a custom leader key and C-return as:
(spacemacs/set-leader-keys "op" 'python-shell-send-region)