Skip to content

Installing and setting up Python with pyenv

This is the backup Python setup

These instructions use pyenv instead of uv. If uv is working for you, use the regular Python installation guide instead.

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 (macOS) or Scoop (Windows) to work at their best with pyenv, we need to install a couple other pieces of software.

brew install openssl@3 readline sqlite3 xz zlib tcl-tk
scoop install 7zip innounp dark

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

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 an older Python! Pyenv makes that process simpler.

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

brew install pyenv

This step is only for people on macOS. 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.

touch ~/.zshrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init - zsh)"' >> ~/.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.

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

Be sure to hit enter after the last one!

pyenv for Windows is less popular than pyenv for macOS, 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 manager like scoop, but... to keep things simple, we'll install it using these instructions here. It requires using PowerShell, but you'll survive. I've copied 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.

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. We're going to use Python 3.13.13, which is new enough to be current but old enough that most data analysis packages should work well with it.

To see which versions are available for you to install, run the following command:

pyenv install -l

Installing Python 3.13.13

The command below asks pyenv to install Python 3.13.13. If pyenv install -l shows a newer 3.13.something release, you can use that instead.

pyenv install 3.13.13

Warning

If you receive the error message configure: error: C compiler cannot create executables, it's because you are using an upgraded macOS 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! We'll update pyenv first, then ask it to install Python 3.13.13.

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, but 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.13.13
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 Python 3.13.something in the list.

Setting Python 3.13.13 as the default Python

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

pyenv global 3.13.13

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 Python 3.13.something 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 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.13.something. 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.13 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.

Installing packages

If you come across some code on the internet that asks you to "install a package" using pip install XXXX, you're now good to go. For example, if you want to install pandas, you just use the following code:

pip install pandas

This installs packages into the Python version you selected with pyenv global.

If pip is being picky, you can run this instead:

python -m pip install pandas

If Windows does not notice a new command after installing a package, run:

pyenv rehash

And everything should work great.

You're all set!

Congrats! 🎉