Search code examples
python-3.xzshzshrcpyenv

Sourcing ./zshrc generates an if statement that considerably slows down shell load time each time


As the title suggests, everytime I open/source my ./zshrc the following code is generated

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

Eventually, I have that code copy&pasted a hundred times (inside my ./zshrc) and considerably slows down my shell load time.

What is this code, and how do I stop it from reappearing?


Solution

  • eval "$(pyenv init -)" initializes pyenv, so you can use it in your shell. pyenv is a tool for installing and managing multiple version of Python.

    To see what it does, just run

    pyenv init -
    

    in your shell. After that, copy-paste the output of the command into your ~/.zshrc file and remove the code you listed above. Your shell will start up much faster after this. Should pyenv ever stop working for you, just run pyenv init - again and update your ~/.zshrc file with its output.

    Oh, and by the way: Doing source ~/.zshrc is generally a bad idea. Instead, if you've modified your .zshrc file and you want to see the results, just restart your terminal or do exec zsh.


    Update

    @akBo I just read your question again and realized that what you are actually saying that the block of code you've posted is added again and again to your ~/.zshrc file.

    Have you perhaps added the following to your .zshrc file somewhere?

    echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.zshrc
    

    This comes from the pyenv installation instructions, but is not something you should add to your .zshrc file. Rather, your meant to run that on your command line once so that the block of code you posted is added to your .zshrc file. But because you've instead added that line to your .zshrc, it gets executed each time your .zshrc gets sourced and thus again and again adds the same block of code.

    Just remove the line I posted above from your ~/.zshrc file and you should be fine.