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
Jedi is "smart enough to figure out which Virtual environment you're currently using"? #199
Comments
In general, if you have your project's virtual environment activated, jedi is smart enough to find it even if jedi isn't installed in that particular virtual environment. It does not sound like you have your virtual environment activated in your emacs environment. That said, if you don't want to activate the environment for some reason, an existing mechanism should work for you: https://github.com/pappasam/coc-jedi#jediworkspaceextrapaths {
"jedi.workspace.extraPaths": ["/home/me/src/my-project/.venv/lib/python3.10/site-packages"],
} This will be the location of the 3rd party packages from your virtual environment. |
@pappasam Thanks for your reply. There is no standard way of "activating" a Python virtual environment in Emacs - there are lots of competing ones. Could you explain the mechanism by which Jedi/jedi-language-server actually works out the virtualenv? I think I must be missing something in terms of what you are assuming. Thanks! |
https://jedi.readthedocs.io/en/latest/_modules/jedi/api/environment.html?highlight=virtualenv# I think it examines the environment for the |
I also don't use emacs. I run neovim from within a terminal, so my workflow entails activating my virtual environment (automatically with zsh hooks) before opening neovim. |
Closing due to lack of activity, hopefully this issue can serve as documentation for anyone with a similar question |
I think even for Neovim users it would be very helpful to have some way to explicitly set the Python interpreter. I usually don't activate my venv(s), I just run Does Jedi Language Server handle any of this, or does it rely entirely on Jedi for "environment detection"? If so, is this an upstream issue, or a feature that can be added directly to Jedi LS? I use Jedi LS instead of the "standard" Python LSP Server because I think the overall experience is better, but this has always been an annoying limitation for me. I'd be happy to at least take a look at the relevant code to see how hard it would be to add this feature. |
@gwerbin Your workflow may be strange to me, but fear not! I think we've got your back (please report back if I'm wrong) :) If you install For example, if you have a virtualenv called |
@pappasam I should have also mentioned that I installed Or have I misunderstood how Jedi LS works? Can it detect libraries from "python B" while itself running under "python A"? |
For anyone watching this issue: My PR #232 adding this feature just merged! And the latest Jedi Language Server release 0.38.0 should finally make this explicitly configurable using the initialization option For any Neovim users, here is how I set it up in my config, with some helper functions to automatically detect the root dir, environment, and extra source paths: https://git.sr.ht/~wintershadows/dotfiles/tree/eafeb864/item/.config/nvim/lua/plugin-config/nvim-lspconfig.lua#L544-770 |
@gwerbin Thanks for this trick. I needed to dynamically determine the virtual environment, and setting it via This might also be worth mentioning for the documentation, as far as I can tell this is the only method to change the virtual environment during runtime. |
In case an emacs user lands here, I'm documenting how I'm currently doing it. I'm installing jedi-language-server using pipx to avoid installing it in every venv. Emacs-side I'm using envrc-mode and a This config needs to use lsp-deferred to "ensure" the variables ( Then jedi-language-server spots the So my setup looks like this: (use-package envrc
:ensure t
:init (envrc-global-mode))
(use-package lsp-mode
:ensure t
:hook (((python-mode) . lsp-deferred)
(rust-mode) .lsp)
:commands (lsp lsp-deferred)
) My VIRTUAL_ENV=.venv
layout python3 the first line makes direnv put the venv inside a As a nice side effect my venvs are automatically activated/deactivated when I enter/exit a directory (from within emacs or not). If you want to dig deeper, here's my .emacs.d/init.el and my .bashrc, at the bottom of the |
The README:
I'm sold on using pipx, but as far as I can see Jedi isn't and can't be smart enough to figure out which virtual environment to use, at least not without some help. If I use jedi-language-server on a folder, there is no way for Jedi to know which virtualenv to use - in fact I might have multiple venvs I'm using for that folder. I've attempted this using lsp-jedi for Emacs, and "jump to definition" fails to work if the item comes from a dependency located in a venv (e.g in
~/.virtualenvs/myproject
).Some clients may be configured with some mechanism for linking projects to venv, but I don't think Jedi can do it by itself.
After browsing the source code of jedi-language-server, I have found just one way to get it to work without changing the source - by creating
.jedi/project.json
in the project root as follows:This is an almost undocumented feature of Jedi it seems - it fact it is wrongly documented as being
.jedi/config.json
.I'm not sure how this is supposed to work, so:
The text was updated successfully, but these errors were encountered: