182

Since I got the macOS v12.3 (Monterey) update (not sure it's related though), I have been getting this error when I try to run my Python code in the terminal:

Python not found error

I am using Python 3.10.3, Atom IDE, and run the code in the terminal via atom-python-run package (which used to work perfectly fine). The settings for the package go like this:

atom-python-run-settings

The which command in the terminal returns the following (which is odd, because earlier it would return something to just which python):

Which Python

I gather the error occurs because the terminal calls for python instead of python3, but I am super new to any coding and have no idea why it started now and how to fix it. Nothing of these has worked for me:

  • I deleted and then reinstalled the Python interpreter from python.org.
  • I tried alias python='python3' (which I saw in one of the threads here).
  • I tried export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" (which I found here).
  • To reset zsh and paths, I deleted all associated hidden files in /local/users/ and ran the terminal once again.
  • I deleted everything and reinstalled Mac OS X and the Python interpreter only to get the same error.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kirill Ignatyev
  • 3,036
  • 3
  • 5
  • 18
  • 1
    Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and take the appropriate [action](https://stackoverflow.com/posts/71591971/edit) (it covers terminal output as well). – Peter Mortensen Jun 26 '22 at 21:07

18 Answers18

355

Anyone updating their macOS to Monterey 12.3 will find that they suddenly no longer have the system-provided Python 2.

The reason for this is that Apple removed the system-provided Python 2 installation (details).

So a workaround/solution for this is to use pyenv to install Python 2.7 (or any other specific version you need).

  1. Install pyenv with brew to manage different Python versions: brew install pyenv
  2. List all installable versions with pyenv install --list
  3. Install Python 2.7.18 with pyenv install 2.7.18
  4. List installed versions with pyenv versions
  5. Set global python version with pyenv global 2.7.18
  6. Add eval "$(pyenv init --path)" to ~/.zprofile (or ~/.bash_profile or ~/.zshrc, whichever you need)
  7. Relaunch the shell and check that Python works, or run $ source ~/.zprofile (Thanks masoud soroush!)
Bernd Kampl
  • 4,837
  • 4
  • 21
  • 26
119

OK, after a couple of days trying, this is what has worked for me:

  1. I reinstalled Monterey (not sure it was essential, but I just figured I had messed with terminal and $PATH too much).
  2. I installed python via brew rather than from the official website. It would still return command not found error.
  3. I ran echo "alias python=/usr/bin/python3" >> ~/.zshrc in terminal to alias python with python3.

Problem solved.

As far as I get it, there is no more pre-installed python 2.x in macOS as of 12.3 hence the error. I still find it odd though that atom-python-run would call for python instead of python3 despite the settings.

Kirill Ignatyev
  • 3,036
  • 3
  • 5
  • 18
  • 24
    In some cases a program that you are using doesn't look into your `~/.zshrc`. In my case a more portable solution was to run `ln -s /opt/homebrew/bin/python3 /usr/local/bin/python`, which doesn't depend on zsh. – Ava May 02 '22 at 05:57
  • 2
    `echo "alias python=/usr/bin/python3" >> ~/.zshrc` is the right command so it appends a new line before the previous content in the ~/.zshrc file – Alex Arvanitidis Aug 11 '22 at 11:21
  • You do not need to define an alias if you install Python 3.x using Pyenv, for example. See my answer https://stackoverflow.com/a/73772425/505650 for a working solution. – Jarno Lamberg Sep 19 '22 at 18:38
  • 1
    Works but in my case the path is `/usr/local/bin/python3`. – Robin Nov 04 '22 at 08:08
  • If you are on an Apple Silicon , instead, see my answer: https://stackoverflow.com/a/74356785/12794085 Brew does not install casks to the directory you are pointing to. – berkaykurkcu Nov 08 '22 at 07:03
56

If you simply installed Python 3, just use python3 as the command instead of just python. In my case, I had to install pynev first via Homebrew (executable brew) using brew install pyenv.

But still after using pynev to install Python 2.7.18 and setting it as a global version using pyenv global 2.7.18, I still ran into an error while trying to run python.

What worked for me (since I already had Python 3 installed) was by changing my command to use python3 instead of just python. Of course, this won't be a solution to everyone who may want to use Python 2.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alvin
  • 920
  • 3
  • 9
  • 1
    Thanks, adding the ```3``` behind ```Python``` made the commands work! I wonder why this is...anyone an explanation? – MikhailRatner Jun 29 '22 at 13:54
  • How Can I just us python as a command that looks much simpler. – Subhan Ahmed Jul 30 '22 at 15:57
  • 1
    It appears you can use an alias (there are some caveats, though), @SubhanAhmed check out: https://askubuntu.com/questions/320996/how-to-make-python-program-command-execute-python-3 – Alvin Jul 31 '22 at 11:57
  • Your comment addresses only one of the 2 issues.The problem with installing a proper python version and calling it from the terminal has been resolved in one of the earliest answers. What I am left with now is why `atom-python-run` keeps calling for `python` instead of `python3` despite the settings. Please be sure you read the problem and the accepted answer before contributing. – Kirill Ignatyev Aug 04 '22 at 06:13
35

you should try

sudo ln -s /usr/bin/python3 /usr/local/bin/python

that solve my problem:)

eddie a
  • 359
  • 3
  • 2
16

I just had this problem on a new Macbook Pro with macOS Monterey, and the below worked for me using Homebrew.

Using alias is not necessary when using Pyenv. Tested with Atom 1.60.0 and atom-python-run 0.9.7.

  1. Install pyenv (https://github.com/pyenv/pyenv#installation) and its dependencies (https://github.com/pyenv/pyenv/wiki#suggested-build-environment):

    brew install pyenv
    brew install openssl readline sqlite3 xz zlib tcl-tk
    
  2. Install Python 3.10.6 but I assume other 3.x versions should work as well:

    pyenv install 3.10.6
    
  3. Add Pyenv to your shell according to the instructions in https://github.com/pyenv/pyenv#set-up-your-shell-environment-for-pyenv. In your home directory:

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
    echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
    echo 'eval "$(pyenv init -)"' >> ~/.zshrc
    
  4. Make the installed Python available everywhere (this can be overridden per project or folder, if necessary):

    pyenv global 3.10.6
    

Now, the output looks like this:

% which python
/Users/jl/.pyenv/shims/python

% python --version
Python 3.10.6

If some scripts still fail, check that you have added Pyenv to the necessary shell startup file(s) as mentioned in step 3 above.

Jarno Lamberg
  • 1,530
  • 12
  • 12
  • Every time I try to run `pyenv install 3.10.6` I get prompted to install XCode Developer Tools, which is already installed. It says "The 'clang' command requires the command line developer tools." The error in Terminal says: `make: error: sh -c '/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -sdk '' -find make 2> /dev/null' failed with exit code 34304: (null) (errno=No such file or directory)` I'm on Monterey. – Corey Sep 30 '22 at 04:49
  • Also failing for me % pyenv install 3.10.7 python-build: use openssl@1.1 from homebrew python-build: use readline from homebrew Downloading Python-3.10.7.tar.xz... -> https://www.python.org/ftp/python/3.10.7/Python-3.10.7.tar.xz Installing Python-3.10.7... python-build: use tcl-tk from homebrew python-build: use readline from homebrew python-build: use zlib from xcode sdk BUILD FAILED (OS X 12.6 using python-build 20180424) Inspect or clean up the working tree at /var/folders/7s/rdl105q14k5b9d7cqc9l1gsr0000gn/T/python-build.20221010101606.15912 Results logged to /var/folde – Jayden Lawson Oct 10 '22 at 00:16
  • @Corey Even if XCode Developer Tools are already installed, does the error message go away if you run the below command and then try to re-install the Python version? `xcode-select --install` – Jarno Lamberg Oct 10 '22 at 23:03
  • @JaydenLawson Same as my previous comment to Corey: does that work for you? – Jarno Lamberg Oct 10 '22 at 23:04
  • You may need to restart your Terminal to see the change. – Tom J Nov 05 '22 at 09:26
11

Try the command python3 --version if you have installed Python 3.

Ravindu Perera
  • 307
  • 2
  • 5
10

Since you installed python3.9> (https://python.org/downloads/) , you just need to run everything with python3 instead of python. You don't have to downgrade your python to use it... It works fine with suffixing python with 3, i.e. python3 eg. $ which python3, python3 --version, $ python3 script.py etc

ewuraba
  • 111
  • 1
  • 4
  • Your comment addresses only one of the 2 issues (and still brings nothing to table as you're not the first one to recommend that in this thread). The problem with installing a proper python version and calling it from the terminal has been resolved in one of the earliest answers. What I am left with now is why `atom-python-run` keeps calling for `python` instead of `python3` despite the settings. Please be sure you read the problem and the accepted answer before contributing. – Kirill Ignatyev Aug 04 '22 at 06:17
  • 1
    @Kig please be sure to read [ask] and focus your questions on ***one*** specific problem. Questions here shouldn't be asking multiple questions to begin with... – Tomerikoo Aug 06 '22 at 12:15
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32401300) – Dan Alexander Aug 09 '22 at 03:57
6

I installed Python then this error occurred and I just run

echo "alias python=/usr/bin/python3" >> ~/.zshrc

and I t worked.on macOS Monterey 12.4, Python 3.8.9

yetesfa
  • 305
  • 2
  • 9
5

I had the same problem with fresh MacOS Ventura with Apple Silicon M1 Pro chip. Seems like brew installs to a different location than before on Apple Silicon?

/usr/bin/ folder had a Python executable and the version for it was 3.9.6.

So I installed Python using brew but then I had to execute it via python3 command so I ran echo "alias python=/opt/homebrew/bin/python3" >> ~/.zshrc and restarted my terminal. I was prompted with the desired version of Python when executing python command.

berkaykurkcu
  • 289
  • 2
  • 9
4

Just run the below command in your terminal:

echo "alias python=/usr/bin/python3" >> ~/.zshrc

Basically, here we are saying the terminal to treat python as python3.
Works like magic!

1

I got zsh: Command not found python after installing using https://www.python.org/ftp/python/3.10.4/python-3.10.4-macos11.pkg

I solved it by configuring the ENVIRONMENT. I added a line, alias python=/usr/local/bin/python3.10, into the .zshrc file (you must be sure it is the true path).


My macOS (Apple silicon):

Darwin Johns-MacBook-Pro.local 21.4.0 Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58 PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000 arm64

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CheverJohn
  • 55
  • 1
  • 10
  • @PeterMortensen I mean that you just add 'alias python=/usr/local/bin/python3.10` into the .zshrc file. That is all. – CheverJohn Jun 27 '22 at 03:06
1

alias python=/usr/bin/python3

This command works but need to re-enter whenever we restart the termian

1

If you are using pyenv and on Mac M1, then try switching the python from system version to a version that is installed already (ex: pyenv global 3.8.13, considering 3.8.13 is another version). In order to check what versions installed use pyenv versions.

If you don't have another version, try installing a new one and switch to that new environment (ex: pyenv install 3.8.13). Then install using pip. Everything should work fine. thats what worked for me.

and make sure your pip and python are aliased to pip3 and python3 respective in the respective terminal configuration file.

Suresh
  • 21
  • 6
1

I First installed python with brew

brew install python

Then I tried linking the python with symlinks using:

brew link python3

And it worked for me.

PS: I was trying to run a shell script and alias approach didn't work.

Pannag
  • 136
  • 1
  • 3
-1

I have a MacBook Pro with an Apple M1 chip and macOS v12.4 (Monterey).

  • I deleted all Python versions and cleared the trash
  • Opened Terminal and typed "python"
  • This prompted an xcode.app update
  • Then I downloaded the latest version of Python from python.org (version 2.10.5)
  • Installed that version
  • Quit Terminal
  • Opened Terminal
  • Typed "python3"

It seems to now be there OK.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stuart
  • 315
  • 1
  • 8
-1

I use brew install brew install python@3.9, after installation success I want to know the version of python, I run command python --version, and I got the problem zsh: command not found: python.

If you install it successfully, please check in the terminal. It will show you if you got problem in install if not problem you just change command from python --version to python3 --version.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Your comment addresses only one of the 2 issues (and still brings nothing to table as you're not the first one to recommend that in this thread). The problem with installing a proper python version and calling it from the terminal has been resolved in one of the earliest answers. What I am left with now is why atom-python-run keeps calling for python instead of python3 despite the settings. Please be sure you read the problem and the accepted answer before contributing. – Kirill Ignatyev Aug 04 '22 at 06:17
-1

for 3.10.8

install with python with brew command

brew install python@3.10

if you have .zshrc file just edit like this

alias python='python3'

alias pip ='pip'

export PATH="/opt/homebrew/opt/python@3.10/libexec/bin:$PATH"

if you dont have .zshrc file:

cd

vi .zshrc

and copy/paste the line below

alias python='python3'

alias pip ='pip'

export PATH="/opt/homebrew/opt/python@3.10/libexec/bin:$PATH"

billyboo
  • 7
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 23 '22 at 16:45
-1

eval "$(pyenv init --path)"

bash starting will be more slow, just suggest

export PATH=$PATH:$HOME/.pyenv/shims

Liuer Hei
  • 89
  • 1
  • 5