7

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

1
  • 1
    same issue. vscode 1.66.2 Commented Apr 21, 2022 at 0:59

6 Answers 6

21

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! This answer is the only one that fixed the problem for me!
5

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

Comments

4

The above answers are very useful!

And there is another way:

  1. open Code -> Preferences -> Settings or press command +
  2. search Run In Terminal
  3. Select it!

Comments

2

Much more easy is create a symbolic link via command line (linux, macos). ln -s /opt/homebrew/bin/python3 /opt/homebrew/bin/python

Works for me.

Comments

0

Changing the setting in the code runner extension can solve the issue. Following these steps could work for you:

  1. Open the settings tab beside your code runner extension
  2. Go to Code Runner: Executor map
  3. Click on settings.json below it. enter image description here
  4. The existing python should be "python": "python -u". Change it to "python":"python3"

VSCode Setting

Comments

-1

click on extension in vscode and type "code runner". select the setting on the code runner and click on extension settings and on edit in settings.json change as python3 instead of python. it will work

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.