Skip to content

Instantly share code, notes, and snippets.

@josemarimanio
Created May 13, 2020 12:13
Show Gist options
  • Star 85 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save josemarimanio/9e0c177c90dee97808bad163587e80f8 to your computer and use it in GitHub Desktop.
Save josemarimanio/9e0c177c90dee97808bad163587e80f8 to your computer and use it in GitHub Desktop.
Installing pyenv on macOS for Zsh using Homebrew

Installing pyenv on macOS for Zsh using Homebrew

Published May-13-2020

Installation

Install pyenv and pyenv-virtualenv thru Homebrew

~$ brew update
~$ brew install pyenv
~$ brew install pyenv-virtualenv

Add the following lines to the end of the .zshrc file in your root directory (~)

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
export PIPENV_PYTHON="$PYENV_ROOT/shims/python"

plugin=(
  pyenv
)

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Reload the .zshrc file to apply the changes made

~$ source ~/.zshrc

See all available versions for pyenv

~$ pyenv install --list
Available versions:
2.1.3
2.2.3.
2.3.7
...

Install a Python version from the list above. Note that this may take some time to complete

~$ pyenv install 3.5.6

Check on the installed pyenv Python versions

~$ pyenv versions
* system (set by /User/user/.pyenv/shims/version)
3.5.6

Basic Usage

Switch the global Python to a specific version

~$ pyenv global 3.5.6
~$ python -V
Python 3.5.6

Switch back the global Python to the system version

~$ pyenv global system
~$ python -V
Python 3.7.7

You can also set a project-specific version of Python

~$ pyenv global system
~$
~$ mkdir project
~$ cd project
project$ pyenv local 3.5.6
project$ python -V
Python 3.5.6
project$ cd ..
~$ python -V
Python 3.7.7

Virtualenv

Create a virtualenv for the Python version used for pyenv

~$ pyenv virtualenv 3.5.6 venv35
...
~$ pyenv activate venv35
(venv35) ~$ pyenv deactivate
~$

Check on the installed pyenv virtualenvs

~$ pyenv virtualenvs
venv35 (created from /Users/mari/.pyenv/shims/versions/3.5.6)
@prussyuval
Copy link

To managed to get pyenv working with macOS 11.4 and zsh, you should have in `~/.zshrc':

eval "$(pyenv init --path)"
eval "$(pyenv init -)"

@Gatsby-Lee
Copy link

Thank you

@Gatsby-Lee
Copy link

To managed to get pyenv working with macOS 11.4 and zsh, you should have in `~/.zshrc':

eval "$(pyenv init --path)"
eval "$(pyenv init -)"

Thank you. yours helps me.

Without this, it didn't work

eval "$(pyenv init --path)"

To me, these were enough. then, what are the other variables for?

eval "$(pyenv init --path)"
eval "$(pyenv init -)"

@matevzkren
Copy link

For Zsh (https://github.com/pyenv/pyenv#basic-github-checkout), in my case installed pyenv with brew.

On a fresh install of mac os Monterey (12.0.1) linking shims was unsuccessful. The trick was to first run the command pyenv install, which apparently puts some things in order. You can then proceed with the following edit to your .zshrc file with no changes to .zprofile:

echo 'eval "$(pyenv init --path)"' >> ~/.zshrc

also see:

@b-goldney
Copy link

Thank you @prussyuval - your approach worked for me. Duplicating your approach here for convenience.

Enter this into your .zshrc

eval "$(pyenv init --path)"
eval "$(pyenv init -)"

@AndreaAlessandrini
Copy link

Thanks matevzkren. I can solve my issues using your suggestions.

@phillipsnick
Copy link

Huge thanks for this suggestion

eval "$(pyenv init --path)"
eval "$(pyenv init -)"

Probably saved me a few hours!

@chenlujjj
Copy link

Great instructions !

@sourcecodemage
Copy link

This saved my life! Seriously, I added the .zprofile entries the the pyenv installed listed , but they didn't work. creating .zshrc with the specified commands worked like a charm.

@nest-don
Copy link

nest-don commented Oct 5, 2022

Thank you

@KeithETruesdell
Copy link

Awesome, thank you. This helped with some headaches.
Specifically trying to use the invoke ... commands after starting poetry.
Poetry uses the invoke build and invoke start (from what I understand), which is not supported in Python 3.10+.
Using pyenv i was able get other versions of Python installed to help.

The missing pieces for me were...

  1. Install pyenv virtual env ( brew install pyenv-virtualenv )
  2. Init pyenv virtual env ( adding eval "$(pyenv virtualenv-init -)" to my zsh )
  3. Create and activate a virtual env ( pyenv virtualenv 3.5.6 venv35 and pyenv activate venv35 )

Other mentions that may have helped...
Adding the eval "$(pyenv init --path)" to my zsh
Using export PIPENV_PYTHON="$PYENV_ROOT/shims/python" in my zsh
Using export PATH="$PYENV_ROOT/bin:$PATH" instead of what was on the pyenv docs in my zsh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment