Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode Python Running ommand Error: /bin/sh: python: command not found

I think I'm having path related issues on VSCode.

I completely removed and reinstalled reinstalled the OS, I have Monterey 12.3.1

I installed Python using brew:

➜  ~ python3 --version
Python 3.9.12

➜  ~ which python3    
/usr/local/bin/python3

However I have issues normally running my code on the mac. I can run from the terminal using

➜  test Python3 test.py
Hello World!

but if I try to run using right-clic -> run, I get:

[Running] python -u "/Users/user/Desktop/test/test.py"
/bin/sh: python: command not found

but if I clic on 'Run & Debug' in the left panel I get:

➜  test  /usr/bin/env /usr/local/bin/python3.9 /Users/user/.vscode/extensions/ms-python.python-2022.4.1/pythonFiles/lib/python/debugpy/launcher 56623 -- /Users/user/Desktop/test/test.py 
Hello World!

Weirdly, if I try to run the program with the run button from the touchbar it works:

➜  test  cd /Users/hugo/Desktop/test ; /usr/bin/env /usr/local/bin/python3.9 /Users/hugo/.vscode/extensions/ms-python.python-2022.4.1/pythonFiles/lib/python/debugpy/launcher 56632 -- /Users/hug
o/Desktop/test/test.py 
Hello World!

Could this problem be related to Apple removing Python2 from macOS in Monterey ?

I noticed that the command "python" wasn't working anymore in my terminal so think it might be related to that, but I'm not sure how to solve that in vscode without messing up paths

like image 245
Santeau Avatar asked Jul 10 '26 08:07

Santeau


2 Answers

The issue is with Code Runner. But it's an easy fix. Open Code runner's Executor map settings.json --> find "python" and you'll notice that it says something like "python" == "python -u" --> change this to "python" == "python3" and the code should be running with the shortcuts in the Output window.

Cheers!

like image 187
Belmmost Avatar answered Jul 12 '26 00:07

Belmmost


If you installed python using homebrew, it should be in (by default)

/opt/homebrew/bin/python3

Your issue is actually with the Code Runner. Try to click Run Python File on the top right side of vscode.

enter image description here

Your code will be running well

Another information

macOS since version 10.8 comes with Python 2.7 pre-installed by Apple . 

Information about this https://docs.python.org/3/using/mac.html#:~:text=5.1.-,Getting%20and%20Installing%20MacPython,www.python.org).

So if you run

➜  ~ which python3 

Even if you haven't installed python. By default, show to directory

/usr/local/bin/python3
like image 40
Zaenury Adhiim Avatar answered Jul 12 '26 00:07

Zaenury Adhiim