Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQUEST] Automatically activate a pipenv environment if available #1666

Open
rassie opened this issue Aug 12, 2019 · 12 comments
Open

[REQUEST] Automatically activate a pipenv environment if available #1666

rassie opened this issue Aug 12, 2019 · 12 comments
Labels
is:bug Something isn't working as intended module:lang/python Pertains to Doom's :lang python module
Milestone

Comments

@rassie
Copy link

rassie commented Aug 12, 2019

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.

@seanfarley
Copy link
Contributor

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

@hlissner hlissner added is:feature Adds or requests new features, or extends existing ones module:lang/python Pertains to Doom's :lang python module status:unconfirmed Still under investigation. is:bug Something isn't working as intended status:pending-fix and removed is:feature Adds or requests new features, or extends existing ones status:unconfirmed Still under investigation. labels Aug 13, 2019
@hlissner
Copy link
Member

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?

@rassie
Copy link
Author

rassie commented Aug 15, 2019

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.

@rassie
Copy link
Author

rassie commented Aug 15, 2019

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.

@hlissner
Copy link
Member

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.

@rassie
Copy link
Author

rassie commented May 11, 2020

Any news on this?

@robbert-vdh
Copy link
Contributor

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

@VentGrey
Copy link

VentGrey commented Jun 3, 2021

For anyone who needs a workaround I've managed to get virtual envs + autocomplete / imports fixed with the following command combinations in order. Please mind there might be a shorter way to do this, I just don't know it. I'll assume you already have opened your project in emacs:

  1. Run: M-x lsp
  2. Run: M-x pipenv-activate
  3. Tun: M-x lsp-restart-workspace

That should get your imports, lints and other things inside your virtual env working just fine.

@hlissner
Copy link
Member

hlissner commented Jun 3, 2021

Or just use direnv. Doom provides first class support for it with its :tools direnv module, and it ensures any env initialization is done well before LSP servers are activated.

And it's a lot less hassle than supporting the myriad permutations of python env managers out there.

@VentGrey
Copy link

VentGrey commented Jun 3, 2021

That sounds great :D I didn't know that module existed, my init.el file it's from late 2019 I think. I should go read the latest docs to see how to get that up and running.

@dhruvinsh
Copy link

dhruvinsh commented Aug 26, 2021

Or just use direnv. Doom provides first class support for it with its :tools direnv module, and it ensures any env initialization is done well before LSP servers are activated.
And it's a lot less hassle than supporting the myriad permutations of python env managers out there.

For the pipenv user, on a linux system install direnv. (apt install direnv)

Add eval to ~/.bashrc --> eval "$(direnv hook bash)"

Add .envrc to your project file but do allow the .envrc by issuing direnv allow within that directory.

What goes inside .envrc? Check this out https://github.com/direnv/direnv/wiki/Python
For pipenv just put: layout pipenv

Do not forget to enable direnv in doom config and you are all set.

@VentGrey
Copy link

☝️ This is so useful. I always have to run pipenv-activate when working on a python project. Thanks for sharing this tip!

@hlissner hlissner added this to the Backlog milestone Oct 4, 2021
@hlissner hlissner removed the backlog label Oct 4, 2021
panchoh added a commit to panchoh/dotconfig-doom that referenced this issue May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is:bug Something isn't working as intended module:lang/python Pertains to Doom's :lang python module
Projects
Status: Unreviewed
Development

Successfully merging a pull request may close this issue.

6 participants