python command isn’t working and python3 working on mac

RMAG news

On macOS, it is common to have multiple versions of Python installed, leading to confusion between commands like python and python3. Here’s a detailed guide to understanding and resolving the issue:

Understanding the Issue

python Command: On some macOS systems, python points to Python 2.x.

python3 Command: This typically points to Python 3.x.

Starting with macOS Catalina, Apple does not include Python 2 by default, but you might still encounter environments where python refers to Python 2, while python3 is explicitly Python 3.

Steps to Resolve the Issue

Check Python Versions
Run the following commands to see which versions of Python are installed:

python –version
python3 –version

Use Python 3 Explicitly
If python3 works and python does not, use python3 to ensure you’re using Python 3:

python3 your_script.py

Update Shell Profile to Alias python to python3
You can create an alias in your shell profile to make python refer to python3.

For bash (default on older macOS versions):
Edit your ~/.bash_profile or ~/.bashrc file:

nano ~/.bash_profile

Add the following line:

alias python=python3

Save the file and reload the profile:

source ~/.bash_profile

For zsh (default on newer macOS versions including Catalina and later):
Edit your ~/.zshrc file:

nano ~/.zshrc

Add the following line:

alias python=python3

Save the file and reload the profile:

source ~/.zshrc

Check Python Path
Ensure that the correct Python 3 path is set:

which python
which python3

Install Python 3 if Needed
If python3 is not installed, you can install it using Homebrew:

brew install python

Summary

By creating an alias or using python3 directly, you can avoid issues with the python command not working due to the version mismatch. Adjusting your shell profile ensures consistency across terminal sessions.

Feel free to ask if you need further assistance or clarification on any of these steps.