Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Adding Python to PATH on macOS

00:00 Installing Python on Mac should already add Python to PATH, but that doesn’t mean that actually running Python on Mac is always seamless. One source of confusion is just knowing which command to even use.

00:13 Because on Mac, the basic python command is reserved for running Python 2, which is an old version of Python that used to be pre-installed on macOS.

00:23 It was deprecated in 2020, so you shouldn’t be using it anymore. But because it was pre-installed on Mac, when users went to install Python 3 for their own personal use, they had to use the python3 command so that it didn’t conflict with any internal tools relying on Python 2. So to this day, you’re going to need to run python3 even if Python 2 isn’t installed and the tutorial says to use python.

00:52 Also, if you have multiple versions of Python, you can use specific commands like python3.14 to target a specific version. And finally, if you have an executable to run, you can run it directly from the command line.

01:08 Note that instead of calling the python command and it opening the Python console, I’m just going to use the --version option to print out the version number instead.

01:19 So let’s see what these commands do on my computer. python --version, that command is not found, I don’t have Python 2 installed.

01:31 python3 --version points to Python 3.13. And python3.14 --version points to Python 3.14. So if python3 does work for you and points to the version you want, that’s great. You don’t need to modify PATH.

01:51 But if it doesn’t work or if it points to the wrong version, you will need to modify PATH. And let’s see how to do that now. For me, I want python3 to point to the latest Python version on my computer, which is Python 3.14. So if you’re trying to add Python to PATH, you’re going to need to know where the Python executable is located.

02:15 Now, if the Python commands work for you, you can see where those executables live by using the which command. So which python3 points to this file at /Library/Frameworks/Python.framework inside of that folder.

02:32 That’s the default location where the python.org installer will put your Python executable. If you installed Python via Homebrew or Anaconda, it’s going to have either Homebrew or Anaconda in the path.

02:46 You can also use which with a -a option. So which -a python3, and that’s going to show all of the locations already on your PATH where a python3 executable is found.

03:01 One thing to note, though, is these other ones in /usr/local/bin or /usr/bin. These locations are already automatically added to your PATH, so that’s not going to be very helpful when we want to add a new version.

03:17 Just a note: when the python.org installer downloads Python, it will put the executable inside of /Library/Frameworks, but then add a symbolic link or a symlink inside of /usr/local/bin, which points to the framework location.

03:37 So if I say which python3.14 or even which -a python3.14, that is just showing the /usr/local/bin location. That’s not the path that I’m going to want to put into my PATH when I modify it.

03:55 So if it’s not here, I’m going to need to search my computer. Let’s try to figure out where it is. So you can search by using the mdfind command, mdfind, then python, which is what you’re searching for.

04:09 Then you want to filter the results because that’s going to give you a lot. So pipe, grep, and then /bin/python. So that’s going to filter and show only the results that have /bin/python in the name.

04:24 And /bin is a folder that stands for binary, which is what you would call executables in Unix.

04:32 And on my machine, I have Python 3.8, 3.11, 3.12, 3.13, 3.14.

04:42 The reason I have so many, but they’re not all listed on my PATH, is because they were installed on a different user. And that user, it was added to that user’s PATH.

04:51 And on this user as a realpython user on my machine, only 3.13 is on my PATH. But because there are symbolic links inside of /usr/local/bin, I still can access all of the other ones if I use a targeted version.

05:05 Okay, so if I want to check which of these to actually add, let’s say I want in /Library/Frameworks, let’s add Python 3.14 to double-check that it’s the right file.

05:20 You can just copy that whole path and put it in the command line and run it. And it actually does open up Python 3.14. Okay, so that is the Python executable I’m looking for. I’m going to exit() out of that.

05:34 And I have that path copied to my clipboard. So now we need to add it to what’s called a dotfile, and specifically one that runs automatically when a new terminal window or tab opens up.

05:48 You could call it a shell startup file or a configuration file. So to find those dotfiles inside of your home directory, you can navigate to your home directory by doing cd ~.

06:01 Tilde is the home directory location. And then list the files there using ls -a. The -a option will show even the hidden files, so files that start with a dot, because that’s what we’re looking for.

06:16 These are the different possible dotfiles you’ll want to look for. You should only have one of these. If you have multiple, then use the one that comes highest in this list. So for me, I only have .zprofile.

06:30 So you can look at the contents of .zprofile by using the cat command, cat .zprofile.

06:38 And I can see that Python 3.13 is added to my PATH here. And that’s automatically done using the python.org installer. So we can add a new line to the .zprofile. We’re going to use echo export PATH equals quotes and then your path to your Python with a colon and then $PATH because that will add the existing PATH value to the end so that you’re adding onto PATH, not totally replacing PATH.

07:10 Then you’re going to pipe that into the .zprofile file. So let’s add it here. echo export capital PATH equals quotes.

07:23 Then I’m going to paste that path from before, but I don’t want to point to the executable. So it’s just going to stop at /bin, not even the extra slash there at the end.

07:36 Then colon $PATH end quote. Then two arrows or right angle brackets into .zprofile. Now, if I cat .zprofile and see what’s the contents, this big thing is added to the end.

07:54 And it’s much larger because the PATH variable has been expanded to include the full value now.

08:02 So now you can say source .zprofile and that will run the file. Also, if you opened up a new window, it would just automatically work. But if I want to keep my current window open, then source .zprofile and python3 --version now points to 3.14 and which -a

08:27 python3 has 3.14 listed first.

08:34 In the next lesson, you’ll see how to do this process in Linux. And in the lesson after that, we’ll look at how to modify PATH in Linux and Mac by editing the dotfile from the command line and understanding how the rest of that PATH gets formed if it’s not inside of a dotfile.

Become a Member to join the conversation.