2024-08-23python

Manage Python Versions with pyenv

Install

# install
curl https://pyenv.run | bash

# update available versions
pyenv update

# list versions available (use grep to filter)
pyenv install -l

# install verion
pyenv install 3.11.9

# set global version
pyenv global 3.11.9

# I'm running zsh, the following alters my .zshrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

# restart shell
exec "$SHELL"

# check things are working
which python # /Users/{username}/.pyenv/shims/python
python --version # Python 3.11.9
pip --version # pip 24.0

pipx

pipx installs and runs python applications in isolated environments.

brew install pipx
pipx ensurepath
sudo pipx ensurepath --global

Virtual Env for Notebooks

  1. Create a virtual env for notebooks using pyenv virtualenv
  2. Install dependencies using a lockfile from the vp-airflow repo
# check installed versions of python
pyenv versions

# create virtual env
pyenv virtualenv 3.11.9 notebooks-3.11.9

# check versions, again. See new notebook python version
pyenv versions

# set local version (adds .python-version to directory)
pyenv local notebooks-3.11.9

# check version again to see version changed to notebook version
pyenv versions

# or
pyenv which python

# install python packages from vp-airflow
# Note, comment out the following:
# plyvel, airflow packages, and potentially pdal if you don't need it
pip install -r /Users/mitchellgritts/Documents/projects/vp-airflow/build/as/requirements.in

References