35

I'm trying to run a python file in VSCode using python3.

I can run using integrated terminal like it says in the microsoft vscode tutorial on python. However, I would like the program to print in the output tab and not take up the terminal window. However I'm getting this error.

enter image description here

The standard code runner config file launch.json, looks like this;

"version": "0.2.0",
"configurations": [
    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal"
    }
]

I've tried to set my python path in VSCode in settings.json

...
"python.pythonPath": "python3",
"code-runner.executorMap": {
    "python3": "/usr/bin/python3"
}

I've also set an alias for python -> python3 (as my ubuntu 20.04 doesn't come with python2 anymore)

alias python="python3"

However, I keep getting the above error. Any Ideas?

4
  • I'm no bash geek, but aliases only last in the session (I think). Commented May 5, 2020 at 18:13
  • If you open a terminl and type which python what do you get? Commented May 5, 2020 at 18:14
  • which python3 returns -> /usr/bin/python3 Commented May 5, 2020 at 18:16
  • which python returns -> python: aliased to python3 Commented May 5, 2020 at 18:16

8 Answers 8

54

Solution in 2024

Step1 : Goto settings of Code Runner extension

Step2 : Find the section

Code-runner: Executor Map

And click on

Edit in settings.json

Step3 : Now change the setting for python

Before

"code-runner.executorMap": {
    ...
    "python": "python -u"
    ...
}

Change this to

"code-runner.executorMap": {
    ...
    "python": "python3 -u"
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

It works. However, I have a question. If I simply specify python3 -u as the runner, it will use the default interpreter of the OS, I think. This will cause a potential problem, the interpreter of the runner is not the same as the one of the virtual env for the project?
26

NOTE: no longer working solution. See solution above

Nearly had it. This code

"python.pythonPath": "python3",
"code-runner.executorMap": {
    "python3": "/usr/bin/python3"
}

should be

"python.pythonPath": "python3",
"code-runner.executorMap": {
    "python": "/usr/bin/python3"
}

(The difference is at the beginning of line 3)

1 Comment

FYI - I think this is no longer working with the latest version of vscode: "python.pythonPath" has been deprecated: github.com/microsoft/vscode-python/issues/11015 The answer below suggests a way to solve this as of 2023: stackoverflow.com/a/73709162/3185183
14

In the terminal of vscode, type sudo apt install python-is-python3. After the installation is done just run the code again and enjoy.

2 Comments

Is this command for Mac?
@Igor apt is a command on Debian and similar OS's like Ubuntu
2

An alternative solution

"python.pythonPath": "python3",
"code-runner.executorMap": {
    "python": "$pythonPath -u $fullFileName"
},

Comments

2

Those coming later, if you have Code Runner installed, then you have to go into the settings by going into code-runner: Executor Map "Edit in settings.json" and locate the python section

{
"workbench.colorTheme": "Default Dark+",
"code-runner.executorMap": {

    "javascript": "node",
    "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
    "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "php": "php",
    "python": "python -u",
    "perl": "perl",
    "perl6": "perl6",
    "ruby": "ruby",
    "go": "go run",
    "lua": "lua",
    "groovy": "groovy",
    "powershell": "powershell -ExecutionPolicy ByPass -File",
    "bat": "cmd /c",
    "shellscript": "bash",
    "fsharp": "fsi",
    "csharp": "scriptcs",
    "vbscript": "cscript //Nologo",
    "typescript": "ts-node",
    "coffeescript": "coffee",
    "scala": "scala",
    "swift": "swift",
    "julia": "julia",
    "crystal": "crystal",
    "ocaml": "ocaml",
    "r": "Rscript",
    "applescript": "osascript",
    "clojure": "lein exec",
    "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
    "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
    "racket": "racket",
    "scheme": "csi -script",
    "ahk": "autohotkey",
    "autoit": "autoit3",
    "dart": "dart",
    "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
    "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
    "haskell": "runhaskell",
    "nim": "nim compile --verbosity:0 --hints:off --run",
    "lisp": "sbcl --script",
    "kit": "kitc --run",
    "v": "v run",
    "sass": "sass --style expanded",
    "scss": "scss --style expanded",
    "less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
    "FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
}

}

change this

"python": "python -u",

or something similar so that it looks like this

"python": "python3",

this is because this is new command to run python code in Python 3

Comments

0

Just set the python3 path in linux by pasting this line before your code in visual studio:

#!/usr/bin/python3

Comments

0

I faced the same problem and tried the above solutions by changing

"python": "python -u"

to

"python": "python3 -u"

in settings.json of the code runner extension. However, I still faced the same error. As naive it may sound, the thing that actually solved my problem was just to click on "Run Python File" instead of "Run Code" when you left-click the arrow on the play button.

Comments

-1
sudo apt install python-is-python3

type the above command line and it will work.

1 Comment

This solution was already provided by someone. Duplicate answers are not helpful.

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.