Why Multiple Versions Are Needed
Modern projects often rely on different Python releases for compatibility checks. Tox, the test runner, requires the interpreter paths to be available on the system. Having a clean, isolated installation for each version prevents interference between builds.
Homebrew: Installing the Latest Releases
Homebrew keeps the newest Python 3.x packages in the core repository. Install them with:
brew install python@3.5
brew install python@3.6
brew install python@3.7
brew install python@3.8
pyenv: Lightweight Version Switching
pyenv manages multiple Python binaries without altering system paths. Install pyenv via Homebrew and add the shims to your shell:
brew install pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
source ~/.zshrc
# Install desired versions
pyenv install 2.7.18
pyenv install 3.4.10
pyenv install 3.5.10
pyenv install 3.6.15
# Set global or local version
pyenv global 3.6.15
Conda Environments for Isolated Builds
If you need a separate Python 3.5 build that differs from Homebrew’s, use conda. Create an environment and link it to tox:
conda create -n py35 python=3.5
conda activate py35
# Verify interpreter
python --version
# Add to tox.ini
[virtualenv]
python=python
# or specify path
[tox]
envlist = py35,py36
[testenv:py35]
basepython = /Users/you/miniconda3/envs/py35/bin/python
Configuring tox to Find All Interpreters
Tox scans the PATH for executables named pythonX.Y. Ensure all shims are in the PATH or explicitly list them in tox.ini. Example of a comprehensive configuration:
[tox]
envlist = py26,py27,py34,py35,py36,py37,py38
[testenv]
deps = -rrequirements.txt
commands = pytest
[testenv:py26]
basepython = python2.6
[testenv:py27]
basepython = python2.7
[testenv:py34]
basepython = python3.4
[testenv:py35]
basepython = python3.5
[testenv:py36]
basepython = python3.6
[testenv:py37]
basepython = python3.7
[testenv:py38]
basepython = python3.8
Takeaway: Combine Homebrew, pyenv, and conda to install, isolate, and reference multiple Python versions, then point tox to each interpreter for parallel testing.
People also ask
Can I keep Homebrew’s Python 3.5 while installing 3.4?
Yes; Homebrew only installs the latest patch for each major release. Use pyenv or conda to get older 3.4 binaries.
How do I avoid conflicts between system and Homebrew Python?
Place Homebrew’s shims before /usr/bin in PATH and use pyenv init to manage precedence.
Inspired by a public discussion on Stack Overflow. This article is an original explanation for learners.