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 | bash git 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"' >> ~/.zshrc echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc echo '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-project pyenv local myenv
That’s it. Never think about Python environments again.