How to add Python to the PATH variable in Mac

PATH variable

The PATH variable is a list of directories where each directory​ contains a UNIX executable file (or its alias) for a command/program.

When a command is entered in the terminal, it searches for an executable file with the same name as the entered command in the PATH variable. In the event that the required file is not found, the terminal will respond​ with an error message saying that the command was not found.

One way to overcome this error is to write the complete directory of the executable file (or its alias) instead of just entering the command name. This, however, is not a very user-friendly approach.

An easier way to avoid this error is to add the executable files’ directory to the PATH variable. This often needs to be done after installing Python.

The complete path of the Python (or Python3) UNIX executable can be added (for OS X 10.8 Mountain Lion and up) by:

  1. Opening the Terminal and entering the command: sudo nano /etc/paths. Enter your password when prompted to do so.

  2. A list of directories that are currently a part of the PATH variable will appear. Enter the path of the Python install directory at the end of this list.

  3. Press control + X to quit and then Y to save the changes.

Python can now be used directly from the Terminal without having to write its location every time. Try executing the command python --version to output the default version of Python installed on your system.

Use python3 --version to find out the version of Python3.x.

svg viewer

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved