The shortest guide to working with Python virtual environments
No boilerplate, no fluff, just the essentials. Here’s the correct way to use Python virtual environments. My way. No other way is good. Don’t think about doing it any other way.
- Install pyenv and pyenv-virtualenv
curl https://pyenv.run | bashgit clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv - Add environment variables to your startup files. This is for zsh.
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrcecho '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrcecho 'eval "$(pyenv init -)"' >> ~/.zshrc - Install a Python version you care about
pyenv 3.11 - Create a virtual environment
pyenv virtualenv 3.11 myenv - Activate the virtual environment and set it to always run from this folder
mkdir my-python-projectpyenv local myenv That’s it. Never think about Python environments again.