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
-
pythonCommand: On some macOS systems,pythonpoints to Python 2.x. -
python3Command: 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
python3works andpythondoes not, usepython3to ensure you're using Python 3:
python3 your_script.py
-
Update Shell Profile to Alias
pythontopython3You can create an alias in your shell profile to makepythonrefer topython3.
-
For
bash(default on older macOS versions):
Edit your~/.bash_profileor~/.bashrcfile:
nano ~/.bash_profileAdd the following line:
alias python=python3Save the file and reload the profile:
source ~/.bash_profile -
For
zsh(default on newer macOS versions including Catalina and later):
Edit your~/.zshrcfile:
nano ~/.zshrcAdd the following line:
alias python=python3Save 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
python3is 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.
Top comments (2)
I faced this issue when i first use python on my macbook.