4 ms·
Ask HN: How do you customize Emacs for Python?
I've been using emacs, happily, for over twenty years.
At one point I used VSCode. I didn't hate it, and for JavaScript it can be great. But I'm unhappy with the MS telemetry and it feels bloated when you use too many extensions.
I have been playing with Zed. It's great. But, I can't get a firm answer on how to use venv and asdf together and usage has stalled for me.
Can people share their emacs .emacs configs? I really would love to use type hinting, lsp and code completion inside emacs. But, I keep running into configurations that want to switch to vi key bindings, and I get demoralized.
- david-j-vujic 2y agoI’m using Elpy because it comes with great code evaluating features (sending expressions to the REPL and such). In this config there’s also auto discovery of virtual environments. Adding to that: black, flake8 (not yet added ruff). In the docs, you will find more about this setup with IPython integration, if the code evaluation things is something you look for too. https://github.com/DavidVujic/my-emacs-config https://github.com/DavidVujic/my-emacs-config
- setopt 2y ago> great code evaluating features (sending expressions to the REPL and such) It’s definitely not as advanced as Elpy, but it’s worth noting that the built-in Python-mode also has basic support for this: C-c C-p to start an interpreter (can be reconfigured to run IPython), C-c C-e to send one statement to the interpreter, and C-c C-c to send the entire file to the interpreter. If you install code-cells.el you can get another keybinding to send one “code cell” (delimited by “# %%” comments like VSCode does) to the interpreter. There’s also the “comint-mime” package that enables Matplotlib support in the interpreter so you can see inline plots. An alternative to all this, is the emacs-jupyter package which integrates a Jupyter kernel as the Python REPL in Emacs. > not yet added ruff This was pretty plug-and-play for me if you’re interested: https://github.com/erickgnavar/flymake-ruff https://github.com/erickgnavar/flymake-ruff
- david-j-vujic 2y agoThank you! I will try these ones out.
- timonoko 2y agoWhat is wrong with "apt install python-mode"? I dont get it. My only remarkable addition is (global-set-key (kbd "M-+") 'hippie-expand) which helps the use of long self-explanatory variable names.
- xrd 2y agoI'm usually a Linux user but I'm on macos right now. Is that just a python metapackage that installs all the things you need? Thanks for the response.
- timonoko 2y agoThis seems to be the bare mininum to run that python-mode automagically. (setq auto-mode-alist ; trigger python mode automatically (cons '("\\.py$" . python-mode) auto-mode-alist)) (setq interpreter-mode-alist (cons '("python" . python-mode) interpreter-mode-alist)) (autoload 'python-mode "python-mode" "Python editing mode." t)
- xrd 2y agoI'll try this. I love emacs but wish there was a way to use package-install to set that up. This is confusing to me. I'm not sure how lsp gets initialized, for example.
- juxtapose 2y agoActually, with emacs 29 it should come out of the box. Open any python file (emacs has a built-in python-mode), M-x eglot, and you are golden.
- setopt 2y agoKeep in mind that there are, unfortunately, at least two packages called “python-mode”. There’s one that is shipped with Emacs, which is maintained by the Emacs maintainers. That’s the one I use and I agree with you that it works very well. Then there’s a third-party one maintained by some Python project maintainers. Probably, that might be the one “apt install python-mode” will get you. There is also a “python-ts-mode”. That’s just the built-in Emacs python-mode where the syntax highlighting is replaced with TreeSitter, which provides similar highlighting to other editors like Atom and NeoVim. In addition, there are some heavier alternatives like elpy and anaconda-mode. But I think most people these days use one of the python-modes with LSP. Unless you have a good reason to use one of the others, I’d start with the built-in python-mode. It doesn’t really require any setup in itself, just open a *.py file and it’s active without any use-package statement required. It is however quite bare bones, so you probably want to couple it with e.g. Eglot, Company, Comint-Mime, Code-Cells.el, etc. to get more of a VSCode-like experience in Emacs.