pyenv

pyenv is a Python version manager. In other words, it lets you manage multiple versions of Python on your system. It is unobtrusive and very easy to use. In conjunction with tox, you can test your Python projects on different interpreter versions. It is also very useful to keep track on the latest stable and unstable releases. pyenv is an essential tool that any pythonista should consider having in their toolbox.

Install pyenv with Homebrew:

brew install pyenv

Update your ZSH configuration:

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

List the Python versions:

pyenv install --list

Install the latest stable Python version (currently 3.10.2):

pyenv install 3.10.2

And make sure your default Python is this version:

pyenv global 3.10.2

pipx

pipx installs and runs Python apps in isolated environments. It is pretty useful to install Python command-line interface apps like youtube-dl without affecting your system Python environment.

brew install pipx
pipx ensurepath

To install a package:

pipx install pycowsay

To run it:

pipx run pycowsay these arguments are all passed to pycowsay!

Poetry

Poetry is a project and package manager that natively supports virtualenvs.

Install the preview version which is quite stable and provides interesting features like plugins:

curl -sSL https://install.python-poetry.org | python3 - --preview

By default, Poetry creates virtualenvs in ~/Library/Application Support/pypoetry directory. Most of text editors are not able to detect them at that path and so fail at autocompletion and other nice features. It is more convenient to configure Poetry to create virtualenvs alongside the project directories:

poetry config virtualenvs.in-project true

If you use python-dotenv in your projects, install poetry-dotenv-plugin:

poetry plugin add poetry-dotenv-plugin

To create a Poetry project:

poetry new my-project

To run a script within the current virtualenv:

poetry run python main.py

To open a shell:

poetry shell