zed icon indicating copy to clipboard operation
zed copied to clipboard

Option to select python environments

Open gksoriginals opened this issue 1 year ago • 41 comments

Check for existing issues

  • [X] Completed

Describe the feature

I am only seeing option to select programming language. How can I select existing conda or venv environments in the editor ?

If applicable, add mockups / screenshots to help present your vision of the feature

No response

gksoriginals avatar Feb 10 '24 05:02 gksoriginals

I had the same questions initially. You can follow these docs to get pyright to point at your venv correctly. https://github.com/zed-industries/zed/blob/main/docs/src/languages/python.md

mchieco avatar Feb 10 '24 16:02 mchieco

@mchieco cheers, this works great! Is there a solution for Zed in such a way that we could do the same behavior as, say, other jetbrains tools have it? Where the .idea/ stores the necessary information needed for a project, without having to create these configuration files by ourselves?

Andree37 avatar Feb 12 '24 15:02 Andree37

Not to my knowledge. It’s not actually the IDE doing the work here, it’s the LSP, and since the venv can be different per repo, i have my config stored on each. but i’m sure you could set a global config if your venv is the same on each

mchieco avatar Feb 13 '24 13:02 mchieco

Can I use conda env? I made a conda configuration as follows, but it does not work.

{
  "venvPath": "/Users/lxy/opt/miniconda3/envs",
  "venv": "/Users/lxy/opt/miniconda3/envs/ml"
}

lxysl avatar Feb 18 '24 16:02 lxysl

Can I use conda env? I made a conda configuration as follows, but it does not work.

{
  "venvPath": "/Users/lxy/opt/miniconda3/envs",
  "venv": "/Users/lxy/opt/miniconda3/envs/ml"
}

sorry, that's correct, it worked.

lxysl avatar Feb 19 '24 03:02 lxysl

Can I use conda env? I made a conda configuration as follows, but it does not work.

{
  "venvPath": "/Users/lxy/opt/miniconda3/envs",
  "venv": "/Users/lxy/opt/miniconda3/envs/ml"
}

sorry, that's correct, it worked.

Can i know how yours work? Cause mine is not working.

kodahrt avatar Feb 20 '24 08:02 kodahrt

Can i know how yours work? Cause mine is not working.

Specify your conda environment path as above in the pyrightconfig.json file and put it in the root path of your project. Restart Zed for the changes to take effect. The Language Server Protocol (LSP) will then be able to resolve it.

lxysl avatar Feb 20 '24 14:02 lxysl

Can I use conda env? I made a conda configuration as follows, but it does not work.

{
  "venvPath": "/Users/lxy/opt/miniconda3/envs",
  "venv": "/Users/lxy/opt/miniconda3/envs/ml"
}

sorry, that's correct, it worked.

Could you make it work with Poetry? Tried the same approach by linking the poetry env directory in pyrightconfig.json but still didn't work.

anesmemisevic avatar Feb 23 '24 23:02 anesmemisevic

{ "venvPath": "/Users/ypm/miniconda3/envs", "venv": "/Users/ypm/miniconda3/envs/mllibs" }

I have put this in pyrightconfig.json at the root and restarted. But still doesn't work.

YashM8 avatar Feb 24 '24 00:02 YashM8

For me the full paths didn't work either what does work is the following. I have my conda envs in the folder /User/daniel/mambaforge/envs (if I ls there I get a list of folders with the names of the environments I created). Let's say I have an env called "great_project". The following is the config for that:

{
  "venv": "great_project",
  "venvPath": "/Users/daniel/mambaforge/envs"
}

dwinkler1 avatar Mar 03 '24 13:03 dwinkler1

Related

  • #8541

Moshyfawn avatar Mar 03 '24 16:03 Moshyfawn

FWIW, I made Conda (Miniforge) work on Zed and used my (base) environment by using the following settings.

{
  "venvPath": "/Users/daniel/miniforge3/",
  "venv": "/Users/daniel/miniforge3/",
  "executionEnvironments": [
    {
      "python": "/Users/daniel/miniforge3/bin/python"
    }
  ]
}

danielcs88 avatar Mar 03 '24 19:03 danielcs88

Following pyrightconfig.json work well for Poetry project:

{
  "venvPath": "/Users/allen.he/Library/Caches/pypoetry/virtualenvs/",
  "venv": "cyberbiz-data-api-edPw3-qJ-py3.8"
}

You can get the path via poetry env info

cccccroge avatar Mar 10 '24 05:03 cccccroge

What if you want to use the currently active conda environment without continuously editing a configuration file?

colinvwood avatar Mar 18 '24 21:03 colinvwood

Following pyrightconfig.json work well for Poetry project:

{
  "venvPath": "/Users/allen.he/Library/Caches/pypoetry/virtualenvs/",
  "venv": "cyberbiz-data-api-edPw3-qJ-py3.8"
}

You can get the path via poetry env info

I use pipenv ,and the fllowing config works well

{
  "venvPath": "/Users/shaojiasong/.local/share/virtualenvs/",
  "venv": "pandas_test-6SWXZJHk"
}

notice that ,you should use full path , don't replace home path with '~'

lesonky avatar Mar 26 '24 16:03 lesonky

Is there a solution for not hard-coding the venvPath? If I'm checking in pyrightconfig.json to my repo and sharing with other team members, everyone will have a different venvPath.

maciejgryka avatar Apr 08 '24 12:04 maciejgryka

On top of the above comment, I manage 3-4 versioned venvs due to library maintenance and would like to be able to swap between them. Hardcoding any, let alone all, would not be ideal.

Basically some version of this for telling pyright or whatever to use as a target?

image

JacobCoffee avatar Apr 18 '24 05:04 JacobCoffee

Following pyrightconfig.json work well for Poetry project:

{
  "venvPath": "/Users/allen.he/Library/Caches/pypoetry/virtualenvs/",
  "venv": "cyberbiz-data-api-edPw3-qJ-py3.8"
}

You can get the path via poetry env info

not work for me, my config as following: 截屏2024-04-19 07 56 19

newtome8888 avatar Apr 19 '24 00:04 newtome8888

Is it possible to use an environment variable like $CONDA_PREFIX ?

{
  "venvPath": "$CONDA_PREFIX/envs",
  "venv": "$CONDA_PREFIX/envs/my_env"
}

This does not work for me :(

Spoutnik97 avatar Apr 23 '24 14:04 Spoutnik97

Look like not working for monorepo for pyproject.toml. For example in a directory: awesome_project (folder) |- api_server (in golang) |- python_sdk (in python) My pyproject.toml is in python_sdk. I have set the pyright section. But Zed doesn't seem to detect it.

venvPath = "." venv = "venv"

aezomz avatar May 09 '24 09:05 aezomz

Doesn't work for mono repo. :(

ayansengupta17 avatar May 12 '24 11:05 ayansengupta17

Anyone get Zed working with pyenv? Can't seem to nail down what the correct pyrightconfig.json should be.

jrosebr1 avatar May 12 '24 13:05 jrosebr1

Okay, for anyone wondering how to get pyenv to play nicely with Zed, here is my example pyrightconfig.json:

{
   "venv" : "pyfintech",
   "venvPath" : "/Users/adrianrosebrock/.pyenv/versions"
}

Additionally, take a look at pyenv-pyright which can help you generate your pyrightconfig.json files.

jrosebr1 avatar May 14 '24 13:05 jrosebr1

Following pyrightconfig.json work well for Poetry project:

{
  "venvPath": "/Users/allen.he/Library/Caches/pypoetry/virtualenvs/",
  "venv": "cyberbiz-data-api-edPw3-qJ-py3.8"
}

You can get the path via poetry env info

not work for me, my config as following: 截屏2024-04-19 07 56 19

@newtome8888 Hi, i use this and it works for me 😔

Try this:

{
  "venvPath": "/Users/liudong/Library/Caches/pypoetry/virtualenvs",
  "venv": "/Users/liudong/Library/Caches/pypoetry/virtualenvs/webserver-P4NjN8WG-py3.12"
}

ItsJimi avatar May 29 '24 08:05 ItsJimi

i have this in my bash profile

if [[ -f "pyproject.toml" ]]; then
  env_path=$(poetry env info -p)
  venv_dir=$(basename "$env_path")
  venv_path=$(dirname "$env_path")
  
  if ! grep -q "\[tool.pyright\]" pyproject.toml; then
    echo "[tool.pyright]" >> pyproject.toml
    echo "venvPath = \"$venv_path\"" >> pyproject.toml
    echo "venv = \"$venv_dir\"" >> pyproject.toml
  else
    if ! grep -q "venvPath" pyproject.toml; then
      sed -i "/\[tool.pyright\]/a venvPath = \"$venv_path\"" pyproject.toml
    fi
    if ! grep -q "venv" pyproject.toml; then
      sed -i "/\[tool.pyright\]/a venv = \"$venv_dir\"" pyproject.toml
    fi
  fi
  poetry shell && clear
fi

it sets

[tool.pyright]

in pyproject.toml if it dosen't exist and solves two problems

  • Pyright
  • Chossing the right venv in terminal launch

khalidelborai avatar Jun 04 '24 17:06 khalidelborai

i have this in my bash profile

if [[ -f "pyproject.toml" ]]; then
  env_path=$(poetry env info -p)
  venv_dir=$(basename "$env_path")
  venv_path=$(dirname "$env_path")
  
  if ! grep -q "\[tool.pyright\]" pyproject.toml; then
    echo "[tool.pyright]" >> pyproject.toml
    echo "venvPath = \"$venv_path\"" >> pyproject.toml
    echo "venv = \"$venv_dir\"" >> pyproject.toml
  else
    if ! grep -q "venvPath" pyproject.toml; then
      sed -i "/\[tool.pyright\]/a venvPath = \"$venv_path\"" pyproject.toml
    fi
    if ! grep -q "venv" pyproject.toml; then
      sed -i "/\[tool.pyright\]/a venv = \"$venv_dir\"" pyproject.toml
    fi
  fi
  poetry shell && clear
fi

it sets

[tool.pyright]

in pyproject.toml if it dosen't exist and solves two problems

  • Pyright
  • Chossing the right venv in terminal launch

tweaking this to work with other venv managers should be easy.

hope it helps others solve the problem until we get it in zed.

also it would be helpful if someone can direct me to where this can be implemented into zed.

i can give it some time later.

khalidelborai avatar Jun 04 '24 17:06 khalidelborai

Any ideas for those using conda as prefer env manager? I am having a similar issues too.

LTalhado avatar Jun 12 '24 12:06 LTalhado

The idea user-work-flow for me would be to cd to the root of the tree I am working on and launch zed from there and it just picks up whatever venv/conda env or whatever is configured, i.e. the first entry when doing which -a python. That would be the definintion of "seamless integration". All the other hacks above seem cumbersome and error prone.

esc avatar Jun 14 '24 13:06 esc

And if I switch conda env for example with conda activate zed would just follow suit and then use the currently active env.

esc avatar Jun 14 '24 13:06 esc

I have created a simple task in the editor that allows me choose from all the virtual envs in python that I have. Upon execution it searches for all the venvs and using fzf lets me select one then creates the pyrighconfig.json in the current directory.

{
    "label": "(Python) : Select Virtual Env",
    "env": {
      "cwd": "$ZED_DIRNAME"
    }
    "command": "~/selectenv.sh",
    "use_new_terminal": true
  }

where selectenv.sh is

#!/bin/bash

search_venvs() {
  local search_path=$1
  if [ -d "$search_path" ]; then
    find "$search_path" -type d -name 'bin' -exec test -e '{}/activate' \; -print 2>/dev/null | sed 's|/bin||'
  fi
}

paths=(
  "$PWD"
  "$HOME/.local/share/virtualenvs"
  "$HOME/Library/Caches/pypoetry/virtualenvs"
  "$HOME/.local/share/pdm/venvs"
  "$HOME/.pyenv/versions"
  "$HOME/.virtualenvs"
  "$HOME/.conda/envs"
)


if [ -n "$CONDA_PREFIX" ]; then
  paths+=("$CONDA_PREFIX")
fi

venv_paths=()
for path in "${paths[@]}"; do
  while IFS= read -r line; do
    venv_paths+=("$line")
  done < <(search_venvs "$path")
done


if [ ${#venv_paths[@]} -gt 0 ]; then
  selected_venv=$(printf '%s\n' "${venv_paths[@]}" | fzf --prompt="Select a virtual environment: ")
  if [ -n "$selected_venv" ]; then
    echo "You selected: $selected_venv"

    venv_name=$(basename "$selected_venv")
    venv_path=$(dirname "$selected_venv")

    cat <<EOL > pyrightconfig.json
{
  "venvPath": "$venv_path",
  "venv": "$venv_name"
}
EOL

    if [ -f "pyrightconfig.json" ]; then
      echo "pyrightconfig.json updated with the selected environment."
    else
      echo "pyrightconfig.json created with the selected environment."
    fi
  else
    echo "No virtual environment selected."
  fi
else
  echo "No virtual environments found."
fi

srbh001 avatar Jun 16 '24 23:06 srbh001