If you regularly use Python and its libraries from the terminal or in your shell scripts, adding Python to the system PATH variable is essential. The first time you run Python after installation, your command may return an error or start a different version of Python than the one you downloaded.
Once you add Python to PATH, the hassle of typing the full path to the Python executable is eliminated. If you get the “Command not found” error message while running Python scripts, follow 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
Why should you add Python to PATH?
PATH is basically a system variable that stores a list of directories. When you enter a command in Terminal, macOS looks for the named binary in the PATH directories. macOS uses colons (:) to separate a series of directories.
Be it 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 the terminal, it looks for files with the same name in the PATH variable. In the end you will receive one Command not found Message when the terminal or command prompt (on Windows) cannot find it.
You can always fix such an error by writing the complete directory of executable files. But this is not a user-friendly approach and definitely a time-consuming approach. Additionally, applications that need to access the directory cannot do so because those applications cannot find it. Instead, you can add the executables directory to the PATH variable and simply type the command name to access it.
By following the steps below and adding Python to the PATH on macOS, you'll be able to run Python scripts, manage packages, and simplify your Python development setup in no time. It basically increases your productivity and is 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 are unable to run your Python commands from the terminal, an error may have occurred during the installation process or afterward. Instead of adding Python to PATH, you can reinstall it and try again by simply running the installer. This should add it to the local path.
Add Python to PATH on macOS via Terminal
You can check the installation location of Python by following the steps below:
- Go to Launchpad on Mac (by pressing command and the Spacebar) and enter terminal.
- type which Python (for Python 2.x) or which python3 and press Enter.
Note the path. If it is /usr/local/bin then you don't need to do anything else, everything should be fine. If the path is not /usr/local/bin, continue to the next steps as you will need to customize the .zshrc file. This is a configuration file that runs every time you start a new terminal session.
- Type the following command and press Enter:
nano ~/.zshrc
This will open the Nano text editor to edit the .zshrc file, a configuration file for the computer environment.
- Add the following line to the .zshrc file:
export PATH="/path/to/python:$PATH"
- 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 must be the actual folder that contains the Python3 file.
- Press Ctrl + O to save the .zshrc file and then click Input. Press Ctrl + X to exit the text editor.
- Enter the following command to reload the /zshrc file and save the changes:
source ~/.zshrc
That's it. You have successfully added Python to PATH on macOS. You can re-enter that echo $PATH Command to check updated PATH variable with Python directory.
Complete your Python projects in no time
This is why you should add Python to PATH
Your Python development setup will be incomplete if you don't add it to the system's PATH. This saves time and effort and increases your productivity on complex Python projects. If you're new to Terminal on Mac, check out our dedicated guide to learn more about it. You can also check some important terminal commands to unlock other advanced tools.
Related
How to uninstall Python on macOS
If you want to remove Python from your Mac, you can learn the steps here