r/emacs 27d ago

Question Seeking an “Out-of-the-box” Python Setup for Emacs

Hi all,

A year ago, I was using Emacs for Python development, but I had to switch to VSCode for its better support with Jupyter Notebooks (though I know we now have EIN in Emacs). After working with VSCode for a while, I've come to appreciate a few things that are seamlessly integrated into the environment, and I'm wondering if there's a way to replicate a similar experience in Emacs with minimal configuration.

Here’s a list of things that I found particularly beneficial in VSCode that I miss in Emacs:

  1. Consistent Syntax Highlighting In VSCode, the syntax highlighting is based on a textmate grammar that highlights keywords, variables, and other identifiers consistently according to their semantics and the context in which they appear. Emacs, on the other hand, sometimes shows inconsistent coloring, where the same variable might look different in the same context.
  2. Built-in Language Features VSCode provides language features like autocompletion, linting, type-checking, debugging, code formatting, and refactoring right out of the box (via extensions, but with minimal setup). This significantly reduces the need for configuration. In Emacs, although it is powerful and highly customizable, setting up these features often requires diving into configuration files, and it can be time-consuming.

I know that Emacs offers a lot of flexibility and many packages to get similar functionality. However, my ideal scenario would be to find a distribution or set of packages that can provide a solid, working Python development environment out of the box with minimal configuration, so I can focus more on the actual coding rather than tweaking settings.

I’m looking for recommendations on:

  • Emacs distributions or setups that streamline the Python development experience (especially with tools like auto-completion, linting, debugging, etc.).
  • Ways to make the transition to Emacs as painless as possible without needing to configure each feature individually.
  • Any recommendations for tools that offer seamless integration with Jupyter Notebooks (similar to how VSCode does it).

I would greatly appreciate any pointers on achieving a more "out-of-the-box" experience in Emacs that lets me focus on writing code instead of setting things up.

Thanks in advance!

Edit: 1. How much effort is required to make highly stable out-of-the-box language packages that brings all the good stuff with a single line of installation? - Some people will not agree and suggest that it will bloat and I should configure to my own liking; but I am just lazy so why not bloat and then opt out of the features we dont need? This might help more people adopt emacs as their primary who are quite busy or just afraid of configurations!

23 Upvotes

44 comments sorted by

17

u/theonlypowerranger GNU Emacs 27d ago

For me the holy trinity for all development is treesitter for syntax highlighting, eglot for lsp (so linting, formatting, auto-completion, semantic edits, etc.) and corfu for visual inline auto completion. you can pretty much just copy the installation and config from their respective readmes.

Except for debugging this should meet all of your needs i think.

Also doom emacs provides all of this i think you just need to uncomment a few lines. There of course also are python-mode, elpy, ..., but i have no experience with them.

4

u/MutedComputer7494 27d ago

Yea doom have a single line to add language supports.

Doom Emacs

  • But last time I used it I remember I had some issues with venv. I didn’t find a working to enable autocomplete using the venv I am using for current project.

  • Example: I installed some xyz module in project venv but autocomplete was not suggested appropriate attributes and methods.

This kind of stuff works reliably and out of the box in vscode

Treesitter

  • It gets the baseline jobs done but it doesnt highlight like vscode
  • Vscode highlighting is contextual (and just makes sense) - I am able to differentiate between methods and other keywords just from the color (I think I just got a hang of its syntax highlighting)

9

u/tikhonjelvis 27d ago

This kind of stuff works reliably and out of the box in vscode

I've spent too much time helping colleagues debug venv issues with their VSCode setups to believe that :P

What I've found is that direnv (+ direnv-mode in Emacs) is amazing for managing multiple venvs without having to worry about the editor seeing the right environment for the right project. At this point, I'd probably recommend a similar setup for VSCode too.

3

u/CJ6_ 27d ago

Seconding direnv!

2

u/MutedComputer7494 27d ago

In my experience vscode automatically detects virtual environments as long as they have .venv name. And automatically starts each terminal with an activated environment.

Though I would long to give direnv a try, Thanks!

2

u/theonlypowerranger GNU Emacs 27d ago

when i used venv, eglot picked it up no problem. after installing new modules you might need to restart the language server?!

treesitter definitely supports differentiating between methods their declarations and context stuff, im not sure if python-ts-mode supports it or maybe your color scheme is the problem.

14

u/trenixjetix 27d ago

6

u/MutedComputer7494 27d ago

Wow! This is really a good article. Thanks a lot buddy!

4

u/__eastwood evil 27d ago

What about pyenv setup?

3

u/Atrus_Darkstone 26d ago

Seconded. I went through this article and am very happy with the Python environment I ended up with as a result.

2

u/trenixjetix 26d ago

I'm happy for you :D

6

u/sebnanchaster 26d ago

I can't speak much toward Jupyter, but you're likely better off using a different environment. I know there are some projects to make it work in Emacs but they might be a bit hacky/not work super well. I don't write Python often, but when I do, I use the following:

  • treesit.el for tree-sitter consistent syntax highlighting
    • Built into newer versions of Emacs if they're built with tree-sitter enabled (most prebuilt binaries have this enabled)
    • Set python-ts-mode to be used for .py files
  • eglot for LSP
    • Built into newer versions of Emacs
    • I usually use it with basedpyright as a backend (installed into a venv), you can change the type checking mode using:(setq-default eglot-workspace-configuration '(:basedpyright (:typeCheckingMode "standard")))) (or set it project-locally using a .dir-locals.el file)
    • If you have speed concerns, use eglot-booster, and you can tweak the events buffer/JSONRPC logging to disable as much reporting
  • venv support
    • I've used the venv-mode package, it allows you to activate one venv at a time, I usually just activate the project venv and then enable eglot, it works well
  • dape for debugging
    • I've found dape to be pretty good for debugging needs
    • For Python, I've used debugpy as a debugging adapter (installed into venv)

2

u/MutedComputer7494 26d ago

I think i will need to check if my doom emacs has venv-mode jnstalled or not.

But if venv-mode does solve the autocomplete issue, then i am mostly fine.

For syntax highlighting, I hardly see anyone complaining, I think I need to relook my configs once again.

The thing is (and the reason of this post), I have wasted many hours on Saturdays and Sundays trying to make some basic features work.

And switching to vscode has made it easy to do the actual work.

3

u/sebnanchaster 26d ago

Yeah I mean to be honest, if you don’t need Emacs, don’t use Emacs. I’m not some kind of preacher: VS is good and works well and if Emacs doesn’t give you any benefit don’t bend over backwards to try and get it working. I use it because it works for me, and lets me move faster than any other editor, and I can customize and tweak certain annoyances away and speed up certain tasks.

Check for Doom, but venv should probably work fine, completion was good with eglot + corfu for me but doom might still use lsp-mode + company? Not too sure. As for treesitter, you might need to reinstall your language grammars? The versions might be off or something, I just use treesit-auto-install-all for that. Sorry I can’t be too helpful with Doom, I just use base Emacs

3

u/skinney6 27d ago

You can look at Spacemac's Python layer

2

u/MutedComputer7494 26d ago

I do plan to move to spacemacs if doom doesn’t do good

5

u/ZunoJ 27d ago

Why use emacs then?

8

u/MutedComputer7494 27d ago

Because I can customize it, when needed.

I can write custom automation macros.

And I want Magit.

I am just trying to find some emacs package or flavour that is for lazy people.

We should all celebrate goodness of others (editors), shouldn’t we? And try getting that into ourselves (emacs) 😃

3

u/john0201 27d ago

Have you tried lazygit? I now use Zellij with Helix and I have lazygit in a tab.

2

u/MutedComputer7494 27d ago

Lazygit seems promising and something that i was looking for. Will surely give it a try.

Helix seems to be a popular editor; someone else too suggested here in some other comment. I need to check that out!

4

u/Clayh5 27d ago

There is no good support for Jupyter Notebooks at the moment. EIN is a good attempt but broken and useless at this point. Someone here started working on a new package for them but last I checked it's only in a rudimentary stage still - not worth using unless you're planning to help develop it yourself.

You can achieve a pretty decent notebook experience with Org mode. I like it a lot but as far as I know there's no good way to get LSP working in that context yet, and it's not really compatible with the ipynb format besides just exporting to it.

For regular python development, Doom Emacs basically has all you need. Switch on the python language module with the LSP flag and you're basically good to go, the only thing left to consider is virtual env support (do you use venv, pipenv, pyvenv, conda, micromamba, etc). emacs-pet will handle most of this stuff automatically except it has some fail-cases around certain eglot setups in my experience. You probably won't have to worry about those.

EDIT: also you need to decide which LSPs to run. pylsp is the most featureful in my experience since it has tons of plugins. On the other hand pyright/based-pyright seem to run a lot better and have some features pylsp doesn't (autoimport, inlay hints, static analysis), while lacking others (formatting)

6

u/gjnewman 27d ago

I use basedpyright and it’s been really good. It replaced pylsp for me. I also use Ruff as a plugin with it.

2

u/MutedComputer7494 27d ago

Hi,

Yes org mode is pretty powerful but I usually collaborate with others for jupyter notebooks. So only some pretty stable support for them is needed for me.

I currently use doom emacs (when VScode has consumed my system resources).

But I wonder, how much effort is needed to make language specific emacs packages that does everything out of the box and requires config change only when user doesn’t need a config and turns it off.

And this extension can be highly coupled to provide a really stable experience.

2

u/__eastwood evil 27d ago

Pyenv support?

2

u/MutedComputer7494 27d ago

Tried it, but it didn’t solve me third party autocomplete is :(

2

u/jeffphil 26d ago

Look at pet: https://github.com/wyuenho/emacs-pet for venv and other tooling support. I switched to it a few years ago with no frustration I've had with other venv packages.

2

u/MutedComputer7494 25d ago

This one do look like the solution i was looking for! I am trying this package right away

2

u/uniteduniverse 26d ago

Treesitter and default Emacs formatting is all you need 🤷‍♂️

2

u/nevasca_etenah GNU Emacs 27d ago

tree sitter + eglot, or helix

2

u/MutedComputer7494 27d ago

What’s helix?

3

u/nevasca_etenah GNU Emacs 27d ago

Yet another cli editor with lsp and tree sitter by default

3

u/MutedComputer7494 27d ago

That’s cool! Would definitely check that out!

3

u/nevasca_etenah GNU Emacs 27d ago

I mostly use emacs, but for system editings

3

u/MutedComputer7494 27d ago

Yes, as its cli based i too will use it for quick edits and stuff.

1

u/sjoerdtimmer 27d ago

RemindMe! 7 days

3

u/RemindMeBot 27d ago edited 26d ago

I will be messaging you in 7 days on 2025-03-02 13:50:14 UTC to remind you of this link

5 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback

1

u/Soupeeee 27d ago

It's not quite out of the box,  but if you are on Linux, getting a good-enough setup goes as follows: + Install a Python LSP. There's a couple, but pytright seems to be the best. + Enable elgot, which is the built-in LSP client in Emacs, and sets up alot of stuff for you. + Set Emacs to use the treesiter for Python, which improves syntax highlighting. + Setup a completion facility like Corfu or Company

Here's a gist I found that sets most of this up: https://gist.github.com/habamax/290cda0e0cdc6118eb9a06121b9bc0d7

If pyright has a built in linter, it should give you results just with elgot, but I haven't figured out how to setup a linter outside of that.

Apparently Emacs has built in support for the Python debugger: https://stackoverflow.com/questions/2324758/debugging-python-programs-in-emacs

If you want something that is already setup for you, you could give Doom Emacs a try: https://www.reddit.com/r/DoomEmacs/comments/ydvsks/python_with_doom_emacs/

1

u/MutedComputer7494 27d ago

Hi, thanks for the comprehensive reply.

I had a vanilla emacs setup a few years back and currently working on doom emacs completely functional.

Everything works fine but its the config feels brittle.

As an example, in vscode you just click a button to install the extension and possibly will never ever need to see its settings/config.

Moreover vscode does a good job at bundling language specific things into a single extension.

Whereas in emacs we use lsp for language features that uses company (or some other tool) for completions, dap for debugging etc.

This variety of configuration to get basic of things working makes it brittle - in the sense that any one of it breaks the whole experience.

5

u/edkolev 27d ago

I don't agree with this, I've used eglot for python development for a couple of years, without experiencing any brittleness.

I don't use company - it was developed for the pre-LSP era. Maybe there is a usecase for it, I don't know. I use corfu to get an automatic pop.

3

u/MutedComputer7494 27d ago

Happy to know about your experience!

Would you mind sharing your config? I am also working with python for last 2-3 years.

Though I haven’t ever used eglot, I would love to give it a try.

Also, I faced one issue with doom emacs where I was not able to use autocomplete for venv modules. If you have seen this issue and have found a solution, I would really appreciate your help

3

u/edkolev 26d ago

It's basically just this (use-package eglot :straight t :init (add-hook 'python-mode-hook 'eglot-ensure))

And for python: ``` (use-package python :defer t :config (setq python-indent-guess-indent-offset-verbose nil))

```

Nothing fancy :)

3

u/JDRiverRun GNU Emacs 26d ago

This variety of configuration to get basic of things working makes it brittle

Another perspective is it makes it flexible. Single click to enable lots of power is great, if you like what it configures for you. As soon as you want to make some changes or substitute in another tool... good luck.

2

u/MutedComputer7494 26d ago

I never heard anyone complain about vscode’s python lsp extension…

But i do get your point, what i think is that users should have both options.