-
-
Notifications
You must be signed in to change notification settings - Fork 421
Slow shell performance after running pyenv virtualenv-init #259
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
Comments
This issue is caused by a 100ms delay added to every script executed with
All these calls add up to 1s+ of latency. My theory is this is related to a virus scanner or carbonblack. |
More progress. I can reproduce outside of pyenv now. 😄 #!/bin/bash
SUB_SCRIPT=$(mktemp)
if [[ $1 == "--bash" ]]; then
echo "#!/bin/bash" > $SUB_SCRIPT
fi
echo "exit" >> $SUB_SCRIPT
chmod +x $SUB_SCRIPT
for X in $(seq 100); do
$($SUB_SCRIPT)
done
rm $SUB_SCRIPT
Even on my unaffected system, the performance is measurably worse when shebang line exists.
|
All scripts in bin/ are called through `pyenv` therefore the shebang lines is not necessary. On some systems this provides a measurable increase in performance of the shell prompt. Fixes pyenv#259
All scripts in bin/ are called through `pyenv` therefore the shebang lines are not necessary. On some systems this provides a measurable increase in performance of the shell prompt. Fixes pyenv#259
All scripts in libexec/ (excluding pyenv) are called through pyenv, therefore the shebang lines are not necessary. On some systems this provides a measurable increase in performance of the shell prompt. Related to pyenv/pyenv-virtualenv#259
All scripts in libexec/ (excluding pyenv) are called through pyenv, therefore the shebang lines are not necessary. On some systems this provides a measurable increase in performance of the shell prompt. Related to pyenv/pyenv-virtualenv#259
All scripts in libexec/ (excluding pyenv) are called through pyenv, therefore the shebang lines are not necessary. On some systems this provides a measurable increase in performance of the shell prompt. Related to pyenv/pyenv-virtualenv#259
running into the same problem too, on Ubuntu 16.04. |
@Honghe Does your laptop run CarbonBlack? That was the root cause of my performance issue. |
@cmcginty no running CarbonBlack ;( |
Came here looking for a solution too. Added this to my zsh config and saw terrible performance. On Ubuntu 18.04. Removing this |
Experiencing this^ too. |
All scripts in libexec/ (excluding pyenv) are called through pyenv, therefore the shebang lines are not necessary. On some systems this provides a measurable increase in performance of the shell prompt. Related to pyenv/pyenv-virtualenv#259
Similar problem on Debian 9 with zsh. Removing |
any news ? |
What does |
It can automatically activate virtualenvs when a |
So it just removes the need to run |
This is slowing down shell commands. pyenv/pyenv-virtualenv#259
Yes,
Hence, other than adding |
SolutionWarning: the solution is far from perfect, it breaks command Quick Solution
For ZSH and Bash # Init pyenv-virtualenv, but
# unload precmd hook _pyenv_virtualenv_hook
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
# Warning: unloading the following hook breaks command
# `pyenv activate/deactivate`. Please switch to
# `pyenv shell env_name`, `pyenv shell --unset` instead.
if [[ -n $ZSH_VERSION ]]; then
autoload -Uz add-zsh-hook
add-zsh-hook -D precmd _pyenv_virtualenv_hook
fi
if [[ -n $BASH_VERSION ]]; then
PROMPT_COMMAND="${PROMPT_COMMAND/_pyenv_virtualenv_hook;/}"
fi |
Slow shell performance with pyenv-virtualenv enabled can be caused by some antivirus software, in my case it was ESET. I compared two macbooks with same MacOS version (Catalina), one with and one without ESET. On the system with antivirus installed shell was very sluggish, on the other system its responsiveness was normal. |
I'm finding this slow as well. I'm on latest Arch Linux, nothing special about my configuration, and yet: $ time _pyenv_virtualenv_hook
real 0m0.220s
user 0m0.160s
sys 0m0.070s This is on the high end. For me it varies quite a bit, anywhere from 80 ms to 225 ms. This is way too much delay to add to every shell invocation. The solution posted above is useful, but it does mean that activation is no longer automatic. My scripts check that |
I am getting this as well on |
To find out what takes time, we need to get a debug trace of what's running while you perform those actions and how long it takes. For the first point
For the second point, using |
works perfect, thank you so much! |
Here's an alternative if status is-interactive;
pyenv init - | source
pyenv virtualenv-init - | sed 's/--on-event fish_prompt/--on-event fish_preexec/g' | source
end
|
Well, I tried fixing the poor perf issue. Seems like the maintainers are not interested in fixing performance unless it's backward-compatible. So the hacks outside the project remain. 🤷 |
@reegnz I think what is suggested here is to fix the core problem--which is that pyenv is slow to run and isn't very clever with how it runs code--and not add band-aid fixes that break functionality for people. See #451 (comment) |
Seconded, since not everyone uses zsh and most of the conversation has been dominated by zsh-specific solutions. |
Not sure if I'm doing something wrong but this seems to still result in any command -- eg For now, I think I'll just do things manually (adding |
Is there a similar bash workaround that I can use? |
FWIW, I've tried using
and it does speed up the prompt, but it does seem to also lead to to E.g. poetry will not recognize that a virtual environment is activated. |
eval "$(pyenv virtualenv-init - | sed s/precmd/chpwd/g)" When using However, if this method was applied, the virtual environment was not activated immediately because the eval "$(pyenv virtualenv-init - | sed s/precmd/chpwd/g)"
_pyenv_virtualenv_hook |
Just another data point. I was seeing a ~1.5sec delay on every prompt. Had to remove the hook. |
@yogiyojyno96 Thanks, your solution worked great! I've modified it for the fish shell like this
|
In my case, the root problem is that To speed up the shell, we need to make the execution of # Comment out the default command
# eval "$(pyenv virtualenv-init -)"
export PATH="/usr/local/Cellar/pyenv-virtualenv/1.2.1/shims:${PATH}";
export PYENV_VIRTUALENV_INIT=1;
TEMP_PYENV_VIRTUALENV_PROJECT_DIR=$(mktemp /tmp/pyenv_virtualenv_project_dir_$(date +"%Y-%m-%d_%T"))
_pyenv_virtualenv_hook() {
local ret=$?
if [ -f $TEMP_PYENV_VIRTUALENV_PROJECT_DIR ]; then
project_dir=$(cat $TEMP_PYENV_VIRTUALENV_PROJECT_DIR)
else
TEMP_PYENV_VIRTUALENV_PROJECT_DIR=$(mktemp /tmp/pyenv_virtualenv_project_dir_$(date +"%Y-%m-%d_%T"))
project_dir=""
fi
if [[ $project_dir == "" ]]; then
if [ -f .python-version ] || [ -d venv ]; then
echo $PWD > $TEMP_PYENV_VIRTUALENV_PROJECT_DIR
eval "$(pyenv sh-activate --quiet || true)" || . venv/bin/activate 2> /dev/null || true
fi
elif [[ ! $PWD =~ $project_dir ]]; then
echo > $TEMP_PYENV_VIRTUALENV_PROJECT_DIR
eval "$(pyenv sh-deactivate --quiet || true)" || deactivate 2> /dev/null ||true
fi
return $ret
};
typeset -g -a precmd_functions
if [[ -z $precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then
precmd_functions=(_pyenv_virtualenv_hook $precmd_functions);
fi
function shellExit {
rm "$TEMP_PYENV_VIRTUALENV_PROJECT_DIR" 2> /dev/nul
}
trap shellExit EXIT This is just a modification of the output of This method suits my need (i.e. always go to the project root first), but it is not a universal solution. For instance, if we initially On the bright side, this work-around also supports automatically sourcing virtual environment created from |
I am confused. I removed the |
@ardeidae with the eval command removed cd into the directory that you mention.
I expect the first echo won't show anything and the 2nd will print the virtualenv name. |
The only benefit I see is the "test" string added in the prompt, but without the eval, it seems the used virtual environment is the expected one. If I use |
I don't think I know all the details around this issue, but I believe that the virtualenv is not actually activated. When you call But it's technically not equivalent to actually having activated the env as shown by the absence of If e.g. starting neovim in that directory, it won't pick the python from within that environment. And I anticipate there are similar other corner cases, but this issue isn't the right place to discuss this further, I think. |
@hassec |
Emphasis mine, just doing this blind obscures the problem, not solves it. The solution is to actually improve the performance of So, for the sake of a fresh look at this investigative process: If we can assume that we know what's a libexec and what isn't, and that we don't have to depend on the idea of someone else overriding pyenv or this plugin's behavior, a fix can be seen as follows:
The first check still takes 20-40 millis. Can we do any better? Littering the scripts with Which tells me that the actual prefix finding script isn't a large deal. Digging deeper, Logically speaking, the answer is yes-- it's effectively the result of https://github.com/pyenv/pyenv/blob/5b4d5a32d343dcae5e7b3f1a09850312f89ba868/libexec/pyenv-version-name. The current versions (assuming no other plugins) can change under the result of the following commands:
So, the ideal would be
I'll try to implement this ideal over the next weekend (only focusing on zsh and by extension that the changes are not magic, bash , sorry) on a branch; and this should shave off the majority (>90ms/120ms per call) by my estimate. |
Any reactions from devs? |
If you hover the reaction icon under the last post, you'll see mine in the list. We'll happily accept a PR. To do anything ourselves, we need a way to reproduce the issue or a debug trace with timestamps from someone who experiences it. |
Thanks, I understand. My situation: just doing the pyenv virtualen init in my bashrc on Ubuntu 24.04 caused my prompt to appear very slow in wezterm terminal. To notice I held my enter key down for 5 seconds in the terminal and got several empty lines between each prompt. |
I'm getting a ~200ms delay at each prompt. It is all coming from
|
It looks like there is some issue on my system that is causing very poor runtime performance after running
pyenv virtualenv-init
. Related to #132I think the issue is with
pyenv verision-name
. What steps can I take to debug this further?The text was updated successfully, but these errors were encountered: