zed
zed copied to clipboard
Option to select python environments
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
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 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?
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
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"
}
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 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.
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.
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.
{ "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.
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"
}
Related
- #8541
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"
}
]
}
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
What if you want to use the currently active conda environment without continuously editing a configuration file?
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 '~'
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
.
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?
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:
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 :(
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"
Doesn't work for mono repo. :(
Anyone get Zed working with pyenv
? Can't seem to nail down what the correct pyrightconfig.json
should be.
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.
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:
@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"
}
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
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.
Any ideas for those using conda as prefer env manager? I am having a similar issues too.
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.
And if I switch conda env for example with conda activate
zed
would just follow suit and then use the currently active env.
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