Skip to content

Installing and setting up Python

Warning

If you run into problems using the command line, you might want to take a look at troubleshooting on the command line.

Prerequisites

Open the command line

First, you'll want to open up the command line.

Click the magnifying glass to open up Spotlight, then search for Terminal. Run it.

Find Cmder, which we set up before, and run it.

Previously installed Pythons

You can run the following command exactly as it is written below to list all of the Pythons your command line can see.

which -a python

If it says something like python not found, congratulations! You have zero Pythons installed!

The first one in the list is the one that's used when python is typed.

This is just for reference: copy and paste the results somewhere. If we run into problems later, it might help us figure out what's going on.

That's it, you're good to go!

Another option is to use the Start Menu to open up Add and Remove Programs. Scroll down to find anything starting with Python.

If you find any, it might make sense to uninstall them now. I've especially had issues with old Python versions fighting new Python versions on Windows.

Package manager

One way to install software on your computer is to download software. Another way is to run commands on the command line, which uses a tool called a package manager.

Since we think the command line is cool, we're going to use that technique. To do that we need to install some software that allows us to install software from the command line

We'll be installing Homebrew from https://brew.sh/. They have a long line of text on their homepage that you cut and paste into Terminal, but to make your life easy I've reproduced it here:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

It might ask for your password by saying something like Password: []. Type your computer password and press enter.

If you type and type and nothing shows up... I promise, just type your password and hit enter! The command line is hiding your typing from anyone who might be looking at your screen.

We'll be installing Scoop from https://scoop.sh/. The front page isn't perfect, so I'll just copy the directions here:

  1. Use the Start Menu to open up Windows PowerShell. It's another command line! Windows is crazy!
  2. Copy and paste Set-ExecutionPolicy RemoteSigned -Scope CurrentUser into PowerShell and press enter.
  3. It will ask you if you want to change the "Execution policy." Type Y and hit enter.
  4. Copy and paste irm get.scoop.sh | iex into PowerShell and press enter.

Confirm it worked

To confirm Homebrew was installed, type the following command to check the documentation for the brew command.

brew help

Did it work? Great, you're good to go.

To confirm Scoop was installed, type the following command to check the documentation for the scoop command.

scoop help

Did it work? Great, you're good to go.

Additional software

For Homebrew (OS X) or Scoop (Windows) to work at their best, we need to install a couple other pieces of software with them.

brew install readline xz
scoop install 7zip innounp dark

If you're getting an error when installing software with scoop, try reading the scoop issues page.

What do they do? Why do we need them? Who knows, not me! It's just ✨magic✨.

Confirm it worked

Skim over what's written in the last twenty or so lines of the command line. Do you see any errors? Any "command not found?" If not, you're probably okay.

Installing a Python installer

Now we're going to install the command pyenv. Pyenv allows us to juggle different Python versions easily.

For example, maybe you normally use the newest version of Python because it has great features. But then one day your colleague sends you some awful code that only works on old old old Python! Pyenv makes that process simple.

The traditional pyenv is for OS X only. Install by running the command below.

brew install pyenv

This step is only for people on OS X. Windows folks can skip ahead.

Now we need to make sure pyenv sets itself up every single time we open up a command line. To do this, you need to figure out what specific kind of command line you're using. Run the following command to find out:

echo $SHELL

What did it print out?

Copy and paste the following command to add setup instructions to ~/.zshrc, the setup file for the Z shell.

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

Be sure to hit enter after the last one!

Copy and paste the following command to add setup instructions to ~/.bash_profile, the setup file for the Bash shell.

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

Be sure to hit enter after the last one!

pyenv for Windows is less popular than pyenv for OS X, but it works the same way.

We used to install this using scoop, but it looks like there are some problems with that these days! We could use choco, which is another package manage like scoop, but... to keep things simple, we'll install it using these instructions here. It requires using PowerShell – another command line! – but you'll survive. I've cut and pasted the command here for reference, but you can check the documentation if you run into any problems:

Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"

If you want to try the choco approach, here's how to install choco and here's how to install pyenv with choco. It might be a good solution in the long run since choco can be pretty convenient for installing packages (like scoop is supposed to be!).

Confirm it worked

To confirm pyenv was installed correctly, run the following command to ask for its documentation.

pyenv help

If it prints anything other than "command not found" you're good to go.

Installing Python

There are all kinds of different Python versions out there! To see which ones are available for you to install, run the following command:

pyenv install -l

Installing Python 3.11.6

Python 3.12.x is out now (a new one always comes out around the beginning of class in the fall!), but a version of 3.10 or 3.11 is probably a safer bet. With pyenv it's easy to change or downgrade versions, but we don't want to spend all of our time fiddling with things so we'll start with a 3.11 version!

To see what the most recent version of Python you can install is, run pyenv install --list. Scroll up near the top to the one that are just numbers, and pick the best-looking 3.11 you can find. At the time that I'm writing this, 3.11.6 is the most recent.

The command below will install Python 3.11.6.

pyenv install 3.11.6

Warning

If you receive the error message configure: error: C compiler cannot create executables, it's because you are using an upgraded OS X version but some of the more technical parts also need to be updated. You can update this with the command below:

sudo rm -rf $(xcode-select -print-path)
xcode-select --install

Don't worry if it says it's going to take 60 hours to download and install! For some reason it's always lying – it will only take a few minutes.

Once that's done, try running the install command again.

Unfortunately, pyenv on Windows is usually a little behind the most recent releases! You'll need to run pyenv install -l to confirm what the "best" version for you is (just pick the most recent 3.11 version).

This process takes three steps:

  1. First, we'll update pyenv to know about all of the newer versions of Python. This might take a couple minutes – don't worry, it's working!
  2. Then we'll install the Python version we're looking for. It will pop open a Window about installing Python. Just agree to whatever it says.
  3. Finally, we'll use a command called "rehash" to make sure your computer notices the new Python.

The three commands to run one by one are:

pyenv update
pyenv install 3.11.6
pyenv rehash

Warning

Some people have reported the error :: [Error] :: error installing "core" component MSI., which seemingly has to do with something about pyenv and scoop. You'll want to scoop uninstall pyenv, then scroll up on this page to the manual installation using PowerShell.

Info

At the end of the installation there might be a button that says "Disable PATH length limit." Click it and agree to the prompt.

Confirm it worked

Run the following command to see a list of versions installed by pyenv.

pyenv versions

You should see 3.11.6.

Setting 3.11.6 as the default Python

Run the following command to use pyenv to set 3.11.6 as the default Python:

pyenv global 3.11.6

Now you should be all set!

Confirm it worked

First, run the following command to see a list of versions installed by pyenv.

pyenv versions

You should see 3.11.6 again, but this time it should have an asterisk next to it.

To double check, next close your command line and open up a new one command line. Run the following command in the new terminal to check the version of the default Python:

python --version

It should print out Python 3.11.6. Note that the command is python, space, hyphen, hyphen, version (it gets mistyped a lot!).

Finally, we want to confirm that this isn't some other Python 3.11.6 that somehow snuck in. Run the following command in the new terminal to check exactly where the Python lives that is being used.

which python

If the response ends in .pyenv/shims/python, you're good to go! Otherwise, follow the steps up above once again, paying special attention to any commands pyenv tells you to run after installing it.

You're all set!

Congrats! 🎉