astral-sh / rye

a Hassle-Free Python Experience
https://rye.astral.sh
MIT License
12.7k stars 439 forks source link

pyenv interfering with venv #317

Open mhadam opened 1 year ago

mhadam commented 1 year ago

Steps to Reproduce

  1. make sure pyenv is installed, and these lines are in .zshrc, alternatively follow these instructions for your shell:
    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
    echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
    echo 'eval "$(pyenv init -)"' >> ~/.zshrc
  2. create a new project rye init testing
  3. run rye shell

Expected Result

the venv should activate properly, for now I'm just deleting .python-version and the venv activates fine

Actual Result

I've also found that running source ./venv/bin/activate silently fails, seemingly doing nothing.

➜  testing git:(main) ✗ rye shell
Spawning virtualenv shell from /Users/mhadam/Projects/testing/.venv
Leave shell with 'exit'
pyenv: version `cpython@3.11.3' is not installed (set by /Users/mhadam/Projects/testing/.python-version)
virtualenvwrapper_run_hook:12: no such file or directory: /Users/mhadam/.pyenv/versions//bin/python3
virtualenvwrapper.sh: There was a problem running the initialization hooks.

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/Users/mhadam/.pyenv/versions//bin/python3 and that PATH is
set properly.

Version Info

rye 0.4.0
commit: 0.4.0 (cdc5c37bc 2023-05-29)
platform: macos (aarch64)
self-python: cpython@3.10
symlink support: true

Stacktrace

No response

bluss commented 1 year ago

I can't reproduce, so more detailed pyenv setup notes are probably needed. Probably some virtualenv related hook(?)

mhadam commented 1 year ago

ok I've added details, the shell installation needs to be done too just fyi

bluss commented 1 year ago

I think it's related to some configuration of virtualenvwrapper (seen in the error message), but don't know more

mat-mil commented 1 year ago

Why would you use pyenv with rye? Is it not redundant?

mhadam commented 1 year ago

The error is that pyenv doesn't recognize the version that rye is using (it's not installed for pyenv). It's a good question why someone would want to use pyenv but the reality is that many people have it installed and they're going to run into this mysterious error.

So perhaps the question is how should rye try to handle this, if at all?

mat-mil commented 1 year ago

I get it now, thanks. Sure pyenv is a wonderful tool, used that myself (for now).

I would say that maybe the best course of action is to switch entirely to one of the solutions: either pyenv (+ some dependency manager) or rye entirely.

bluss commented 1 year ago

@mhadam Would it be possible for you to look into why anything from pyenv is being called at all in this case? What's in your virtualenv hooks directory, anything pointing to pyenv there? To progress on the bug, can we find reproduction instructions that are verified to work from a clean slate?

mhadam commented 1 year ago

pyenv runs because there's a .python-version file created by rye in the directory

bluss commented 1 year ago

pyenv doesn't run for me with that file, so I'm trying to find the configuration I'm missing to trigger it.

oliverlambson commented 1 month ago

I just had exactly the same issue. At the very least some documentation on how to fix would be good imo.

Steps to reproduce

In a rye-managed project i just cloned from github:

$ source .venv/bin/activate
$ echo $?
0
$ echo $PATH
/Users/oliverlambson/Github/depin/.venv/bin:/Users/oliverlambson/.rye/shims:...
$ rm .python-version 
$ source .venv/bin/activate
$ echo $?
0
$ echo $PATH
/Users/oliverlambson/Github/depin/.venv/bin:/Users/oliverlambson/.rye/shims:...
$ echo $PS1 # the shell prompt update works too now, so whole activate script succeeded
(depin) ...

System info

$ rye --version
rye 0.34.0
commit: 0.34.0 (2024-05-20)
platform: macos (aarch64)
self-python: cpython@3.12.3
symlink support: true
uv enabled: true
$ pyenv --version
pyenv 2.4.1

Configs are set up as-per standard instructions:

# .zshrc
...
# --- Pyenv ---
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH" # only add to path if not found
eval "$(pyenv init -)" # 36ms

# --- rye ---
source "$HOME/.rye/env"
...

Debugging

It seems some pyenv hook is running after the source, which is undoing all the good work the activate script does. In other words, it's not so much failing as it is being overridden by pyenv after succeeding.

Full logs (well, I folded the irrelevant bits):


$ set -x
$ source .venv/bin/activate
+_zsh_highlight_preexec_hook:2> typeset -g _ZSH_HIGHLIGHT_PRIOR_BUFFER=''
...
+omz_termsupport_preexec:1> [[ '' != true ]]
...
+title:1> setopt localoptions nopromptsubst
...
+_ksi_preexec:1> emulate -L zsh -o no_warn_create_global -o no_aliases
...
+-zsh:3> source .venv/bin/activate
+.venv/bin/activate:26> [ '' '=' .venv/bin/activate ']'
+.venv/bin/activate:67> deactivate nondestructive
+deactivate:1> unset -f pydoc
+deactivate:1> true
+deactivate:5> [ -z '' ']'
+deactivate:10> [ -z '' ']'
+deactivate:19> hash -r
+deactivate:21> [ -z '' ']'
+deactivate:27> unset VIRTUAL_ENV
+deactivate:28> unset VIRTUAL_ENV_PROMPT
+deactivate:29> [ '!' nondestructive '=' nondestructive ']'
+.venv/bin/activate:69> VIRTUAL_ENV=/Users/oliverlambson/Github/dipin/.venv 
+.venv/bin/activate:70> [ darwin23.0 '=' cygwin ']'
+.venv/bin/activate:70> [ darwin23.0 '=' msys ']'
+.venv/bin/activate:73> export VIRTUAL_ENV
+.venv/bin/activate:75> _OLD_VIRTUAL_PATH='/opt/anaconda3/bin:/Users/oliverlambson/.rye/shims:/Users/oliverlambson/.cargo/bin:/opt/homebrew/opt/libpq/bin:/Users/oliverlambson/.poetry/bin:/opt/homebrew/Cellar/pyenv-virtualenv/1.2.3/shims:/Users/oliverlambson/.pyenv/shims:~/.npm-global/bin:/Users/oliverlambson/.local/bin:/Users/oliverlambson/bin:/usr/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Applications/kitty.app/Contents/MacOS:/Users/oliverlambson/.orbstack/bin:/opt/homebrew/opt/fzf/bin' 
+.venv/bin/activate:76> PATH='/Users/oliverlambson/Github/dipin/.venv/bin:/opt/anaconda3/bin:/Users/oliverlambson/.rye/shims:/Users/oliverlambson/.cargo/bin:/opt/homebrew/opt/libpq/bin:/Users/oliverlambson/.poetry/bin:/opt/homebrew/Cellar/pyenv-virtualenv/1.2.3/shims:/Users/oliverlambson/.pyenv/shims:~/.npm-global/bin:/Users/oliverlambson/.local/bin:/Users/oliverlambson/bin:/usr/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Applications/kitty.app/Contents/MacOS:/Users/oliverlambson/.orbstack/bin:/opt/homebrew/opt/fzf/bin' 
+.venv/bin/activate:77> export PATH
+.venv/bin/activate:79> [ xdipin '!=' x ']'
+.venv/bin/activate:80> VIRTUAL_ENV_PROMPT=dipin 
+.venv/bin/activate:84> export VIRTUAL_ENV_PROMPT
+.venv/bin/activate:87> [ -z '' ']'
+.venv/bin/activate:92> [ -z '' ']'
+.venv/bin/activate:93> _OLD_VIRTUAL_PS1=$'%(?:%{\C-[[01;32m%}%1{➜%} :%{\C-[[01;31m%}%1{➜%} ) %{\C-[[36m%}%c%{\C-[[00m%} $(git_prompt_info)' 
+.venv/bin/activate:94> PS1=$'(dipin) %(?:%{\C-[[01;32m%}%1{➜%} :%{\C-[[01;31m%}%1{➜%} ) %{\C-[[36m%}%c%{\C-[[00m%} $(git_prompt_info)' 
+.venv/bin/activate:95> export PS1
+.venv/bin/activate:99> alias pydoc
+.venv/bin/activate:99> true
+.venv/bin/activate:108> hash -r
+_pyenv_virtualenv_hook:1> local ret=0                                                                                                                                                                   
+_pyenv_virtualenv_hook:2> [ -n /Users/oliverlambson/Github/dipin/.venv ']'
+_pyenv_virtualenv_hook:3> pyenv sh-activate --quiet
+pyenv:1> local command
+pyenv:2> command=sh-activate 
+pyenv:3> [ 2 -gt 0 ']'
+pyenv:4> shift
+pyenv:7> case sh-activate (activate | deactivate | rehash | shell)
+pyenv:7> case sh-activate (*)
+pyenv:12> pyenv sh-activate --quiet
+_pyenv_virtualenv_hook:3> pyenv sh-deactivate --quiet
+pyenv:1> local command
+pyenv:2> command=sh-deactivate 
+pyenv:3> [ 2 -gt 0 ']'
+pyenv:4> shift
+pyenv:7> case sh-deactivate (activate | deactivate | rehash | shell)
+pyenv:7> case sh-deactivate (*)
+pyenv:12> pyenv sh-deactivate --quiet
+_pyenv_virtualenv_hook:3> eval $'unset PYENV_VIRTUAL_ENV;\nunset VIRTUAL_ENV;\nif [ -n "${_OLD_VIRTUAL_PATH:-}" ]; then\n  export PATH="${_OLD_VIRTUAL_PATH}";\n  unset _OLD_VIRTUAL_PATH;\nfi;\nif [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ]; then\n  export PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME}";\n  unset _OLD_VIRTUAL_PYTHONHOME;\nfi;\nif [ -n "${_OLD_VIRTUAL_PS1:-}" ]; then\n  export PS1="${_OLD_VIRTUAL_PS1}";\n  unset _OLD_VIRTUAL_PS1;\nfi;\nif declare -f deactivate 1>/dev/null 2>&1; then\n  unset -f deactivate;\nfi;'
+(eval):1> unset PYENV_VIRTUAL_ENV
+(eval):2> unset VIRTUAL_ENV
+(eval):3> [ -n '/opt/anaconda3/bin:/Users/oliverlambson/.rye/shims:/Users/oliverlambson/.cargo/bin:/opt/homebrew/opt/libpq/bin:/Users/oliverlambson/.poetry/bin:/opt/homebrew/Cellar/pyenv-virtualenv/1.2.3/shims:/Users/oliverlambson/.pyenv/shims:~/.npm-global/bin:/Users/oliverlambson/.local/bin:/Users/oliverlambson/bin:/usr/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Applications/kitty.app/Contents/MacOS:/Users/oliverlambson/.orbstack/bin:/opt/homebrew/opt/fzf/bin' ']'
+(eval):4> export PATH='/opt/anaconda3/bin:/Users/oliverlambson/.rye/shims:/Users/oliverlambson/.cargo/bin:/opt/homebrew/opt/libpq/bin:/Users/oliverlambson/.poetry/bin:/opt/homebrew/Cellar/pyenv-virtualenv/1.2.3/shims:/Users/oliverlambson/.pyenv/shims:~/.npm-global/bin:/Users/oliverlambson/.local/bin:/Users/oliverlambson/bin:/usr/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Applications/kitty.app/Contents/MacOS:/Users/oliverlambson/.orbstack/bin:/opt/homebrew/opt/fzf/bin'
+(eval):5> unset _OLD_VIRTUAL_PATH
+(eval):7> [ -n '' ']'
+(eval):11> [ -n $'%(?:%{\C-[[01;32m%}%1{➜%} :%{\C-[[01;31m%}%1{➜%} ) %{\C-[[36m%}%c%{\C-[[00m%} $(git_prompt_info)' ']'
+(eval):12> export PS1=$'%(?:%{\C-[[01;32m%}%1{➜%} :%{\C-[[01;31m%}%1{➜%} ) %{\C-[[36m%}%c%{\C-[[00m%} $(git_prompt_info)'
+(eval):13> unset _OLD_VIRTUAL_PS1
+(eval):15> declare -f deactivate
+(eval):16> unset -f deactivate
+_pyenv_virtualenv_hook:7> return 0
+_zsh_highlight_main__precmd_hook:3> setopt localoptions
...
+(eval):1> [[ -o warnnestedvar ]]
+_zsh_highlight_main__precmd_hook:8> _zsh_highlight_main__command_type_cache=( ) 
+_omz_async_request:1> local -i ret=0
...
+is-at-least:23> emulate -L zsh
...
+_omz_async_request:55> read -u 12 '_OMZ_ASYNC_PIDS[_omz_git_prompt_info]'
+(anon):0> return 0
+_omz_async_request:58> zle -F 12 _omz_async_callback
...
+_omz_git_prompt_info:3> __git_prompt_git rev-parse --git-dir
+omz_termsupport_precmd:1> [[ '' != true ]]
...
+title:1> setopt localoptions nopromptsubst
...
+omz_termsupport_cwd:2> local URL_HOST URL_PATH
...
+omz_urlencode:1> emulate -L zsh
..
+omz_termsupport_cwd:3> URL_HOST=Olivers-Laptop 
...
+omz_urlencode:1> emulate -L zsh
...
+omz_termsupport_cwd:4> URL_PATH=/Users/oliverlambson/Github/dipin 
...
+_ksi_precmd:1> local -i 'cmd_status=0'
...
+_ksi_report_pwd:0> print -nu 13 '\e]7;kitty-shell-cwd://Olivers-Laptop/Users/oliverlambson/Github/dipin\a'
+_ksi_precmd:38> print -rnu 13 $'\C-[]2;~/Github/dipin\C-G'
+-zsh:4> git_prompt_info
+git_prompt_info:1> [[ -n $'%{\C-[[01;34m%}git:(%{\C-[[31m%}main%{\C-[[34m%}) %{\C-[[33m%}%1{✗%}%{\C-[[00m%} \n' ]]
...