emacs28 setup for Python with conda/mamba
This setup uses emacs 28. To get it working, you can follow the steps below:
-
Navigate to your home folder and clone the repo, and navigate to the (newly-created)
~/.emacs.d/
folder:cd ~ git clone https://github.com/martibosch/snakemacs ~/.emacs.d cd .emacs.d
-
Use conda/mamba and the
environment.yml
file included in this repository to install the Python and C/C++ dependencies required for autocompletion, syntax checking and function documentation by either:a. installing the requirements in the
base
environment (or any other existing environment provided that it is active at the time of running the commands):# or use mamba instead of conda conda env update -f environment.yml
b. create a new dedicated environment (e.g., named
emacs
) - note that in such case, you must always run emacs from within the dedicated environment:# or use mamba instead of conda conda env create -n emacs -f environment.yml conda activate emacs
-
Install emacs in your system. Currently, only ubuntu builds are supported, but the idea is to eventually (i.e., after several issues are addressed, see the caveats section below) use conda-forge's emacs for a fully conda-based cross-platform setup.
-
Run emacs for the first time from the shell so that all packages can be installed (if you do not run it from the shell,
libvterm
may not be installed properly):emacs
-
From inside emacs, install all the icon fonts
M-x nerd-icons-install-fonts
There are currently two main issues with the emacs from conda-forge, namely an error with newer Linux versions and the lack of native JSON and native compilation, which slow down several features such as lsp. This may be addressed once the v29.1 branch is merged.
Each buffer with Python code (e.g., .py
and .ipynb
files) is associated to a conda/mamba environment. To ensure that IDE features are properly provided, each environment must have a set of packages installed, e.g., pyright for static type checking, black for formatting on save, ruff to check sytnax and style on the fly via flycheck, jupytext to convert Jupyter notebooks to Python scripts...
In order to ensure that these packages are included by default in all environments at the time of their creation, you can set up the create_default_packages
options by adding the following (feel free to adapt the list of packages to suit your needs) to the .condarc
file:
create_default_packages:
- black
- jupytext
- pandoc
- pre-commit
- pyright
- ruff
- ipykernel
There remains nevertheless a caveat with this approach, as these packages will inevitably appear when exporting a conda environment (i.e., conda env export
), adding dependencies that other users trying to reproduce your code may not need.
The conda--infer-env-from-buffer
function of conda.el activates the base environment, i.e., happens if conda-activate-base-by-default
is set to true
(altough it is nil
by default) or if the "auto_activate_base" conda setting is set to true
, which depends on the user's settings. However, snakemacs should instead use the emacs
environment by default. This can either be acheived by (a) contributing to conda.el or (b) overriding the codna--infer-env-from-buffer
function within this configuration (see https://stackoverflow.com/questions/15717103/preferred-method-of-overriding-an-emacs-lisp-function).