Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ask HN: How do you customize Emacs for Python?
3 points by xrd on Sept 22, 2024 | hide | past | favorite | 11 comments
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.



I’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


> 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


Thank you! I will try these ones out.


What 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.


I'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.


This 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)


I'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.


Actually, 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.


Keep 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.


"apt install python-mode" is just verry old (2016) The one in github is from 2023. But both are created in 1992 by Tim Peters.

And they differ greatly:

  diff python-mode.el python-mode.old | wc
  45380  216095 1849879


Aha, I see. The alternative I was thinking of was this Python-mode:

https://wiki.python.org/moin/EmacsPythonMode

It seems that one is on MELPA under the name “python-mode”. It’s a third-party project that replaces the built-in “python-mode”, also actively maintained (last updated last week).




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: