Doom-emacs: [REQUEST] Automatically activate a pipenv environment if available

Created on 12 Aug 2019  路  7Comments  路  Source: hlissner/doom-emacs

It would be really nice if doom-emacs tried to run pipenv-activate on Python files.

Rationale:

  1. Spacemacs does it
  2. It's a noop if a pipenv is not available
  3. (most important) in LSP mode, lsp-mode activates automatically for every file. If the language server is installed into a virtual environment, lsp-mode will not find it without activated virtualenv.

A possible complication is that while lsp-mode starts a language server per workspace, pyvenv applies its configuration globally, so that something like a pipenv-deactivate + pipenv-activate combo is needed every time a buffer is opened or switched to.

:lang python bug pending-fix

Most helpful comment

I'll look into restarting LSP servers when the python environment changes. I don't think this is acceptable on buffer switch, but it makes sense to do so upon a) first entering a python-mode buffer (calling pipenv-deactivate and pipenv-activate on python-mode-hook would happen soon enough for LSP to see the changes) and b) when switching workspaces. I'll look into implementing these.

All 7 comments

Ah, I was just working on this locally. I'm using persp hooks to change the pyvenv (since that's what doom packages).

That it doesn't already is actually a bug. I erroneously thought activating pipenv-mode would activate the pipenv.

Per-buffer switching raises some concerns, however. I imagine something like (add-hook 'doom-switch-buffer-hook #'pipenv-activate) may work, but it sounds terribly expensive. Could you give it a try?

This seems to work reasonably well:

(add-hook 'doom-switch-buffer-hook (lambda ()
                                     (pipenv-deactivate)
                                     (pipenv-activate)
                                     ))

This can get quite slow in some cases, since activating a virtualenv can trigger additional actions apart from setting PATH and other variables. However, I can't really think of a better solution if we can't have per-buffer environment.

However, it's not enough: when restoring a session, LSP mode is activated, but pipenv-activate is not being run. There should be another hook which runs on new buffer or something like that.

I'll look into restarting LSP servers when the python environment changes. I don't think this is acceptable on buffer switch, but it makes sense to do so upon a) first entering a python-mode buffer (calling pipenv-deactivate and pipenv-activate on python-mode-hook would happen soon enough for LSP to see the changes) and b) when switching workspaces. I'll look into implementing these.

Any news on this?

This is of course not a solution to the issue at all, but I've personally starting preferring just opening an instance of Emacs through pipenv run emacs when I'm working on a project that uses it. The main benefit of this approach is that it works both with any program/text editor and with any virtualenv implementation (Pipenv, Poetry, venv etc.).

Was this page helpful?
0 / 5 - 0 ratings