How to add Python to PATH in macOS | Harper29
Call Us Free 773-759-7945
user heart cart0
order Free Shipping on Orders Over $100

How to add Python to PATH in macOS

If you regularly use Python and its libraries from the terminal or in your shell scripts, adding Python to the system’s PATH variable becomes essential. When you run Python for the first time after installation, your command may return an error or launch a different Python version from the one you downloaded. Once you add Python to PATH, it eliminates the hassle of entering the full path to the Python executable all the time. If you run into a "command not found" error message while running Python scripts, use the steps below to add Python to PATH on your Mac.

Related
How to run ipconfig on a Mac
You can run ipconfig to find out your Mac's IP address

What is PATH?

PATH is basically a system variable that stores a list of directories. When you type a command in Terminal, macOS searches for the mentioned binary in the PATH directories. macOS uses colons (:) to separate a string of directories.

Why should you add Python to PATH?

Whether it’s Windows, macOS, or Linux, every operating system has a variable called PATH that contains a list of directories with executable files. When you enter a command in Terminal, it searches for files with the same name in the PATH variable. You end up receiving a command not found message when the terminal or command prompt (on Windows) can’t find it.

You can always get rid of such an error by writing the complete executable file directory. But that's not a user-friendly approach and it's definitely a time-consuming practice. Also, applications that need to invoke the directory won't be able to, as those applications won't be able to find it. Instead, you can add the directory of executable files to the PATH variable and simply enter the command name to access it.

When you follow the steps below and add Python to the PATH on macOS, you can run Python scripts, manage packages, and simplify your Python development setup in no time. It basically boosts your productivity, and it's one of the first things you can do after installing the latest Python version from the company’s website.

Reinstall Python on macOS

When you install Python on Mac, the system adds it to your path by default. If you can't run your Python commands from the terminal, something may have gone wrong during the installation process or afterwards.

Instead of adding Python to PATH, you can reinstall it and try again by just running the installer. This should add it to the local path.

Add Python to PATH on macOS using the Terminal

You can check the install location of Python by following these steps:

  1. Head to Launchpad on Mac (by pressing Command and the Spacebar) and type Terminal.
  2. Type which python (for Python 2.x) or which python3 and hit Return.
    Find python path© Provided by XDA Developers
    Note the path. If it's /usr/local/bin, you don't need to do anything else. If the path is not /usr/local/bin, then proceed to the next step, as you'll need to tweak the .zshrc file. This is a configuration file that runs every time you launch a new Terminal session.
  3. Enter the following command and hit the Return key: nano ~/.zshrc
    Configure Zshell file© Provided by XDA Developers
    This will open the nano text editor to edit the .zshrc file, a configuration file for the computing environment.
  4. Add the following line to the .zshrc file: export PATH="/path/to/python:$PATH"
  5. Replace path/to/python with the actual Python path. For example, if Python is installed in the /Frameworks/Library/Python.framework/Versions/3.12/bin directory, the correct line would be export PATH="/Frameworks/Library/Python.framework/Versions/3.12/bin:$PATH". The folder you add needs to be the folder that contains the python3 file.
    Enter Python directory in Terminal© Provided by XDA Developers
  6. Press Ctrl+O to save the .zshrc file and then hit Enter. Press Ctrl+X to exit the text editor.
  7. Enter the following command to reload the /zshrc file and save changes: source ~/.zshrc

That’s it. You have successfully added Python to PATH on macOS. You can again type the echo $PATH command to check the updated PATH variable with the Python directory.

Complete your Python projects in no time

Your Python development setup is incomplete if you don't add it to the system’s PATH. Doing so saves time and effort and elevates your productivity during complex Python projects. If you are new to Terminal on Mac, check out our dedicated guide to learn more about it. You can also review several important Terminal commands to unlock advanced tools.

Leave a comment

Please note, comments must be approved before they are published