Installing Python


macOS & Linux

While there a many ways to install Python (a version is even included in macOS), I highly recommend using pyenv. pyenv allows you to seamlessly switch between different Python versions. While for this class there will be no need to do so, in the future, you may find yourself working on two different codebases, each of which is only compatible with a specific version of Python.

Installing pyenv

If you already have homebrew installed, simply run

brew update
brew install pyenv

Otherwise, you can use the automatic installer provided

curl https://pyenv.run | bash

Now, determine which shell you are using, typically either zsh or bash. You can simply run echo $0. If you are using a zsh terminal (most likely the case if you have macOS), run the following commands:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

If you are running bash, run the following commands instead. You may need to run them again for ~/.profile, ~/.bash_profile, or ~/.bash_login if you have those.

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

Installing Python using pyenv

Finally, we can now install any version of Python we want! First, let’s see what versions are available to install by running

pyenv install -l

Neat huh? For this course, we will stick with Python 3.10. Simply run

pyenv install 3.10

which will grab the latest version of 3.10 to install. In the future, if you have to manage multiple versions of Python, you can use the following commands to help you switch between them.

pyenv shell <version> # select just for current shell session
pyenv local <version> # automatically select whenever you are in the current directory (or its subdirectories)
pyenv global <version> # select globally for your user account

Windows

If you have not already done so, I highly recommend setting up the Windows Subsystem for Linux (WSL). To install, right click PowerShell or Command Prompt and “Run as administrator”. Then simply run

wsl --install

Once this is installed, you should be able to open a WSL terminal that acts almost exactly like a Linux machine. As a caveat, though, any files that you interact with within WSL need to stay within the WSL environment. Think of it as a partition on your hard drive. From here, you should be able to follow the above instructions to install pyenv.