Rahul Iyer
Rahul Iyer

Reputation: 21025

Python command not working even though preinstalled on M3 Mac Sonoma

I just purchased a new M3 MacBook Pro.

Python is already installed. If I do "brew install python" then I get a message saying:

Warning: [email protected] 3.12.2_1 is already installed and up-to-date.
To reinstall 3.12.2_1, run:
  brew reinstall [email protected]

But if I run "python", then I get a message saying:

% python
zsh: command not found: python

If I run "python3", then it works fine.

% python3
Python 3.12.2 (main, Feb  6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin

But obviously something isn't right because if I run a python file like:

% ./setup.py
zsh: ./setup.py: bad interpreter: /usr/bin/python: no such file or directory

So what is the best way to proceed? Reinstall using homebrew? (I did not originally install with homebrew - python seemed to come preinstalled with my Mac, or maybe something else installed it) Or use the python installer? Or what?

This is probably a common issue. I am using Sonoma 14.4.1 (23E224) on a MacBook Pro M3 Pro (November 2023 model).

Upvotes: 3

Views: 1676

Answers (2)

lycheejelly
lycheejelly

Reputation: 177

Try out pyenv: it will automagically add both a python and python3 command, and both will point to python 3.

Pyenv lets you have multiple versions of python throughout the system. I, myself, don't need multiple versions of python (python is not my main language), but it certainly helps me to keep my python version up-to-date. Here is the tutorial summed up for the zsh shell.

# install it
brew install pyenv

# set the pyenv_root environment variable, put 
# the executable on the PATH, and set pyenv to start on terminal startup
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

# restart shell
exec zsh

# finally, the good stuff. install python and set the install version
# to the default, globally
pyenv install <version>
pyenv global <version>

# restart shell
exec zsh

# did it work? as long as it is not /usr/bin/python, you are good.
which python

You can use the subcommands shell and local to start a shell in the given version and set the default version of this directory and subdirectories to the given version.

Upvotes: 1

Chris
Chris

Reputation: 36581

You need to install Python 2.x.

brew install python@2

Do not symlink /usr/bin/python to /usr/bin/python3 as there are differences and programs written for Python 2.x may not run with Python 3.x.

Upvotes: 1

Related Questions