There are many ways to install Python. You can ask three people about this topic and you will get five different opinions. My opinionated recommendation is to use pyenv to install and manage multiple Python versions and its companion pyenv-virtualenv to manage virtual environments.
If you are a macOS user like me, you should use Homebrew to install it.
brew update
brew install pyenv pyenv-virtualenv
Tip
If you have not used Homebrew yet, you can install it with this command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then, we append these lines at the end of the ~/.zshrc
file:
# pyenv config
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Usage Link to heading
First, we list all Python versions available to pyenv
on the current system:
pyenv versions
Then, we list all available versions:
pyenv install --list
To create a virtual environment, you can execute a similar command:
pyenv virtualenv 3.11.4 arxiv-3.11.4
This will create a virtualenv based on Python 3.11.4 under $(pyenv root)/versions
in a folder called arxiv-3.11.4
.
If you want to automatically activate a specific virtual environment once you access
a specific directory, you can execute pyenv local arxiv-3.11.4
inside that directory.
This will create a .python-version
which indicates the virtual environment to activate.