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

How to automatically use a conda environment? #42

Open
spring-haru opened this issue Nov 9, 2024 · 3 comments
Open

How to automatically use a conda environment? #42

spring-haru opened this issue Nov 9, 2024 · 3 comments

Comments

@spring-haru
Copy link

I am trying to automatically set a conda environment if a .py file is opened. I wonder if you can help me out. I tried the following

return  {
  "HallerPatrick/py_lsp.nvim",
  config = function()
    require('py_lsp').setup{
      opts = {
        host_python = "~/opt/anaconda3/envs/py12/bin",
        auto_source = true,
        language_server = "pyright",
        source_strategies = "conda",   -- "default", "poetry", "conda", "system",
        default_venv_name = "py12"
      }
    }
  end,
}

but it returns

Could not retrieve python path, try :PyLspReloadVenv

Then, if I do :PyLspActivateCondaEnv, then the cond environment is used in lsp, though :checkhealth shows

py_lsp: require("py_lsp.health").check()

py_lsp.nvim Report ~
- ERROR No client attached
- ERROR Setup is incorrect

How am I supposed to change the code to automatically set the conda environment. Thanks in advance.

@HallerPatrick
Copy link
Owner

Hey! First try to use a table instead of a string for the source strategy, so:

return  {
  "HallerPatrick/py_lsp.nvim",
  config = function()
    require('py_lsp').setup{
      opts = {
        host_python = "~/opt/anaconda3/envs/py12/bin",
        auto_source = true,
        language_server = "pyright",
        source_strategies = { "conda" },
        default_venv_name = "py12"
      }
    }
  end,
}

If that not works, could you please check what conda env list --json returns for you?

@HallerPatrick
Copy link
Owner

I also pushed some changes so you might want to update this plugin

@spring-haru
Copy link
Author

Thanks. I updated the plugin and used a table for the source strategies, but basically no change. Let me summarize what I got:

  • Opening a .py file still shows
Could not retrieve python path, try :PyLspReloadVenv
  • Then, doing :PyLspActivateCondaEnv shows
Using Python Virtual Environment: /Users/my_name/opt/anaconda3/bin/python
  • :checkhealth shows that the conda environment is correctly set for lsp, but it also shows
py_lsp: require("py_lsp.health").check()
py_lsp.nvim Report
- ERROR No client attached
- ERROR Setup is incorrect

where the last line sometimes (seemingly randomly) changes to

- OK Setup is correct

And conda env list --json shows

{
  "envs": [
    "/Users/my_name/.julia/conda/3/aarch64",
    "/Users/my_name/.julia/environments/v1.9/.CondaPkg/env",
    "/Users/my_name/Library/jupyterlab-desktop/jlab_server",
    "/Users/my_name/opt/anaconda3",
    "/Users/my_name/opt/anaconda3/envs/mercury",
    "/Users/my_name/opt/anaconda3/envs/numpy2",
    "/Users/my_name/opt/anaconda3/envs/otter5",
    "/Users/my_name/opt/anaconda3/envs/otter6",
    "/Users/my_name/opt/anaconda3/envs/py10",
    "/Users/my_name/opt/anaconda3/envs/py11",
    "/Users/my_name/opt/anaconda3/envs/py12",
    "/Users/my_name/opt/anaconda3/envs/pymc",
    "/Users/my_name/opt/anaconda3/envs/rise"
  ]
}

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

2 participants