Skip to content

Latest commit

 

History

History
132 lines (102 loc) · 4.11 KB

PYENV.md

File metadata and controls

132 lines (102 loc) · 4.11 KB

PyEnv

Install / Update

brew update
brew install pyenv

DO NOT ENABLE PYENV BY DEFAULT

Do not install it to your .zshrc or .bashrc

Activate on demand

This causes the least magic 🔮* and you have better control over what is activated and when.

eval "$(pyenv init --path)"

Avoiding magic

Click here for a deeper dive into avoiding *magic 🔮

The Python environmental protection agency wants to seal it in a cement chamber, with pictorial messages to future civilizations warning them about the danger of using sudo to install random Python packages.

*magic 🔮 --> refers to pip install poetry or pip install awscli could resolve to installing it into ANY of MANY different python versions that might exist on your PATH and this all depends on the order they resolve.

So when trying to upgrade it can get problematic...

For example you install poetry into homebrew/python3.8 but then homebrew upgrade python3 and that now resolves to hombrew/python3.9. poetry is still resolving first on PATH but pip install --upgrade poetry is pointing to homebrew/python3.9.

The main way to avoid this completely and over the top is to use the python3 -m pip install --upgrade poetry format of the command, but use the absolute path to the version of python you need like:

/opt/homebrew/bin/python3.8 -m pip install --upgrade poetry
# OR
$HOME/.pyenv/shims/python3.8 -m pip install --upgrade poetry

List Versions you CAN install

pyenv install --list | grep "  3\.9"

  3.9.0
  3.9-dev
  ...
  3.9.14

List Versions that ARE installed

pyenv versions                      

  system
  2.7.18
  3.6.15
  3.7.13
* 3.8.12 (set by /Users/joshpeak/.pyenv/version)
  3.8.13
  3.9.10
  3.10.3

Check and Activate a Version

Checking currently activated version

pyenv version

3.8.12 (set by /Users/joshpeak/.pyenv/version)

Activating a new globally set version

pyenv global 3.10.3

pyenv version      

3.10.3 (set by /Users/joshpeak/.pyenv/version)

PyEnv Troubleshooting

  1. Always use python3 and not python. This could resolve to a python2 installation.
  2. Use which -a python3 to figure out ALL python3 binaries that could resolve from PATH environment variables and in which order.
    which -a python3  
    /Users/joshpeak/play/python-onboarding-guide/.venv/bin/python3
    /opt/homebrew/bin/python3
    /usr/bin/python3
    /opt/homebrew/bin/python3
  3. When deactivateing a poetry shell sometimes you need to unset POETRY_ACTIVE to clean the environment variables.
  4. Use python3 -m site to triage if a library is not importing correctly. This lists the exact paths an import will traverse. It always has the current directory first, then system libraries that the virtual env links to and shares, and finally the locally installed libraries in your .venv
    sys.path = [
        '/Users/joshpeak/play/python-onboarding-guide',
        '/opt/homebrew/Cellar/.../3.10/lib/python310.zip',
        '/opt/homebrew/Cellar/.../3.10/lib/python3.10',
        '/opt/homebrew/Cellar/.../3.10/lib/python3.10/lib-dynload',
        '/Users/joshpeak/play/python-onboarding-guide/.venv/lib/python3.10/site-packages',
    ]
    USER_BASE: '/Users/joshpeak/Library/Python/3.10' (doesn't exist)
    USER_SITE: '/Users/joshpeak/Library/Python/3.10/lib/python/site-packages' (doesn't exist)
    ENABLE_USER_SITE: False

Resources