简体   繁体   中英

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

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.

在此处输入图像描述

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM