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

Pyright sticks to the first virtual enviroment you open #66

Open
axeoman opened this issue Dec 29, 2021 · 4 comments
Open

Pyright sticks to the first virtual enviroment you open #66

axeoman opened this issue Dec 29, 2021 · 4 comments

Comments

@axeoman
Copy link

axeoman commented Dec 29, 2021

I have multiple python projects with different virtual environments in <project_root>/venv directory.
With a help of .dir-locals.el and projectile-project-root, whenever I open a file it will run pyvenv-activate to the right venv.
I have pyrightproject.json with "venv": "venv" inside.

However, pyright is kinda "sticks" to the environment of the project that was first opened after emacs restart, e.g. if I open a file from project A and then open a file from project B: project B files will have unresolved library imports error because these libraries are not installed in the environment A.

What am I missing? Not sure if it is a bug or I just need to tweak lsp-pyright / pyrightproject.json more (I have tried using executionEnvironments there but behaviour was the same).

@vakker
Copy link

vakker commented Mar 3, 2022

I'm having the exact same issue using pyenv-mode. Did you find a solution?
After some digging and trying to manually set lsp-pyright-venv-path that is used in lsp-pyright-locate-python I still can't make it work.

It seems like there's only 1 workspace created, e.g. if I open a file then LSP reports that:

LSP :: pyright:972173 initialized successfully in folders: (/home/user/project1 /home/user/project2 /home/user/.pyenv)

then if I open another file from the other project:

LSP :: Connected to [pyright:972173]

Is that correct?

Here it says that the server will ask for configs for each workspace, but that doesn't seem to happen.

@deifactor
Copy link

As a workaround, if you set (setq lsp-pyright-multi-root nil) then it'll run separate processes. You need to set that before you (require 'lsp-pyright), and may need to lsp-workspace-folders-remove your existing folders. You can check whether this is working if lsp-describe-session shows different port numbers for the different pyright instances.

@erikcw
Copy link

erikcw commented Jan 12, 2024

I'm running into the same issue. Setting (setq lsp-pyright-multi-root nil) early in my config does not seem to help. I removed all workspace folders as started from scratch. Looks like each time I switch into a new project, it gets "appended" to the session from the first virtual environment.

@vljosa-vivodyne
Copy link

Hope this helps someone:

I don't fully understand why, but I found that I had to set the python.pythonPath setting of pyright correctly for each project (virtualenv):

(setq lsp-pyright-multi-root nil) ; Must be set before lsp-pyright is loaded. https://github.com/emacs-lsp/lsp-pyright/issues/19#issuecomment-930596080

(defvar lsp-client-settings (make-hash-table :test 'equal)) ; Original does not set :test, so values with string keys cannot be replaced.

(use-package lsp-pyright
  :ensure t
  :config
  (lsp-register-custom-settings
   '(("python.pythonPath" ljosa-lsp-pyright-locate-python)))

  (define-key lsp-mode-map (kbd "C-c C-l") lsp-command-map)
  :hook (python-mode . (lambda ()
                         (require 'lsp-pyright)
                          (lsp))))

I also had to make sure that projectile understood that the virutalenv were in diferent projects. (This was relevant because they were in the same git repo.) I did this by creating empty .projectile files in the root of each project.

After this, it still doesn't work at first: I have to do M-x lsp-reload-session the first time I open a file in a new project (virtualenv).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants