'How to run python3 code in VSCode? /bin/sh: 1: python: not found
I'm trying to run a python file in VSCode using python3.
I know I can fix by simply setting to 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.
The standard coder runner 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?
Solution 1:[1]
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)
Solution 2:[2]
In the terminal of vscode, type sudo apt install python-is-python3
. After the installation is done just run the code again and enjoy.
Solution 3:[3]
An alternative solution
"python.pythonPath": "python3",
"code-runner.executorMap": {
"python": "$pythonPath -u $fullFileName"
},
Solution 4:[4]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Erick Mwazonga |
Solution 4 | Mungo Prus-shearer |