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

Option to select python environments #7646

Open
1 task done
gksoriginals opened this issue Feb 10, 2024 · 33 comments
Open
1 task done

Option to select python environments #7646

gksoriginals opened this issue Feb 10, 2024 · 33 comments
Labels
enhancement [core label] language An umbrella label for all programming languages syntax behaviors python Python programming language support

Comments

@gksoriginals
Copy link

Check for existing issues

  • 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 gksoriginals added admin read Pending admin review enhancement [core label] triage Maintainer needs to classify the issue labels Feb 10, 2024
@mchieco
Copy link

mchieco commented Feb 10, 2024

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

@Andree37
Copy link

@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?

@mchieco
Copy link

mchieco commented Feb 13, 2024

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

@JosephTLyons JosephTLyons added python Python programming language support language An umbrella label for all programming languages syntax behaviors and removed triage Maintainer needs to classify the issue admin read Pending admin review labels Feb 13, 2024
@lxysl
Copy link

lxysl commented Feb 18, 2024

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
Copy link

lxysl commented Feb 19, 2024

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.

@kodahrt
Copy link

kodahrt commented Feb 20, 2024

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.

@lxysl
Copy link

lxysl commented Feb 20, 2024

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.

@anesmemisevic
Copy link

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.

@YashM8
Copy link

YashM8 commented Feb 24, 2024

{ "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.

@dwinkler1
Copy link

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"
}

@Moshyfawn
Copy link
Contributor

Related #8541

@danielcs88
Copy link

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"
    }
  ]
}

@cccccroge
Copy link

cccccroge commented Mar 10, 2024

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

@colinvwood
Copy link

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

@lesonky
Copy link

lesonky commented Mar 26, 2024

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 '~'

@maciejgryka
Copy link

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.

@JacobCoffee
Copy link

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

@newtome8888
Copy link

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

@Spoutnik97
Copy link

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 :(

@aezomz
Copy link

aezomz commented May 9, 2024

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"

@ayansengupta17
Copy link

Doesn't work for mono repo. :(

@jrosebr1
Copy link

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

@jrosebr1
Copy link

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.

@ItsJimi
Copy link

ItsJimi commented May 29, 2024

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"
}

@khalidelborai
Copy link

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
Copy link

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.

@Ltalhado
Copy link

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

@esc
Copy link

esc commented Jun 14, 2024

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
Copy link

esc commented Jun 14, 2024

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

@srbh001
Copy link

srbh001 commented Jun 16, 2024

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
Copy link

srbh001 commented Jun 17, 2024

A preview of this task in zed
ezgif-7-db247d0efb

rgbkrk added a commit that referenced this issue Jun 17, 2024
Run any Jupyter kernel in Zed on any buffer (editor):

<img width="1074" alt="image"
src="https://github.com/zed-industries/zed/assets/836375/eac8ed69-d02b-4d46-b379-6186d8f59470">

## TODO

### Lifecycle

* [x] Launch kernels on demand
* [x] Wait for kernel to be started
* [x] Request Kernel info on start
* [x] Show in progress indicator
* [ ] Allow picking kernel (it defaults to first matching language name)
* [ ] Menu for interrupting and shutting down the kernel
* [ ] Drop running kernels once editor is dropped

### Media Outputs

* [x] Render text and tracebacks with ANSI color handling
* [x] Render markdown as text
* [x] Render PNG and JPEG images using an explicit height based on
line-height
* ~~Render SVG~~ -- not happening for this PR due to lack of text in SVG
support
* [ ] Process `update_display_data` message and related `display_id`
* [x] Process `page` data from payloads as outputs
* [ ] Render markdown as, well, rendered markdown -- Note: unsure if we
can get line heights here

### Document

* [x] Select code and run
* [x] Run current line
* [x] Clear previous overlapping runs
* [ ] Support running markdown code blocks
* [ ] Action to export session as notebook or output files
* [ ] Action to clear all outputs
* [ ] Delete outputs when lines are deleted

## Other missing features

The following is a list of missing functionality or expectations that
are out of scope for this PR.

### Python Environments

Detecting python environments should probably be done in a separate PR
in tandem with how they're used with LSP. Users likely want to pick an
environment for their project, whether a virtualenv, conda env, pyenv,
poetry backed virtualenv, or the system. Related issues:

* #7646
* #7808
* #7296

### LSP Integration

* Submit `complete_request` messages for completions to interleave
interactive variables with LSP
* LSP for IPython semantics (`%%timeit`, `!ls`, `get_ipython`, etc.)

## Future release notes

- Run code in any editor, whether it's a script or a markdown document

Release Notes:

- N/A
fallenwood pushed a commit to fallenwood/zed that referenced this issue Jun 18, 2024
Run any Jupyter kernel in Zed on any buffer (editor):

<img width="1074" alt="image"
src="https://github.com/zed-industries/zed/assets/836375/eac8ed69-d02b-4d46-b379-6186d8f59470">

## TODO

### Lifecycle

* [x] Launch kernels on demand
* [x] Wait for kernel to be started
* [x] Request Kernel info on start
* [x] Show in progress indicator
* [ ] Allow picking kernel (it defaults to first matching language name)
* [ ] Menu for interrupting and shutting down the kernel
* [ ] Drop running kernels once editor is dropped

### Media Outputs

* [x] Render text and tracebacks with ANSI color handling
* [x] Render markdown as text
* [x] Render PNG and JPEG images using an explicit height based on
line-height
* ~~Render SVG~~ -- not happening for this PR due to lack of text in SVG
support
* [ ] Process `update_display_data` message and related `display_id`
* [x] Process `page` data from payloads as outputs
* [ ] Render markdown as, well, rendered markdown -- Note: unsure if we
can get line heights here

### Document

* [x] Select code and run
* [x] Run current line
* [x] Clear previous overlapping runs
* [ ] Support running markdown code blocks
* [ ] Action to export session as notebook or output files
* [ ] Action to clear all outputs
* [ ] Delete outputs when lines are deleted

## Other missing features

The following is a list of missing functionality or expectations that
are out of scope for this PR.

### Python Environments

Detecting python environments should probably be done in a separate PR
in tandem with how they're used with LSP. Users likely want to pick an
environment for their project, whether a virtualenv, conda env, pyenv,
poetry backed virtualenv, or the system. Related issues:

* zed-industries#7646
* zed-industries#7808
* zed-industries#7296

### LSP Integration

* Submit `complete_request` messages for completions to interleave
interactive variables with LSP
* LSP for IPython semantics (`%%timeit`, `!ls`, `get_ipython`, etc.)

## Future release notes

- Run code in any editor, whether it's a script or a markdown document

Release Notes:

- N/A
@pavelzw
Copy link

pavelzw commented Jun 23, 2024

The pixi package manager also creates conda environments and stores them in $PWD/.pixi/envs/<env-name>.

Adding something like

{
  "venvPath": ".pixi/envs/",
  "venv": "default"
}

works for pyright and IDE integration but terminal.detect_venv currently doesn't work with conda environments (and thus pixi environments) at all. There, you would need to run pixi shell or eval "$(pixi shell-hook)" to activate the environment.

@sapiosaturn
Copy link

sapiosaturn commented Jun 26, 2024

in case anyone is having trouble resolving imports for a conda env even after setting the venv and venvPath to the correct values, what fixed it for me was also explicitly specifying the pythonVersion in the project pyrightconfig.json file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement [core label] language An umbrella label for all programming languages syntax behaviors python Python programming language support
Projects
None yet
Development

No branches or pull requests