介紹
這裏小虎介紹8種方法利用vscode運行python的代碼,親測有效,可以參考使用。請在配置好環境後展開。
小技巧
在當前文件夾下,下面可以用vscode打開當前文件夾編程。
code.
Run
method 1
記得選右下角編譯器。
method 2
在編輯窗口按鼠標右鍵。
method 3
框選代碼,然後鍵盤輸入下面幾句。此法用於測試少數幾行代碼,跟斷點測試不同,斷點是從前往後,此法可運行只運行中間幾行。
shift + enter
method 4
打開command palette,然後輸入選擇Python: start REPL。這跟直接在terminal裏面輸入python是一樣的。
shift + Ctrl + P
Python: start REPL
然後在terminal輸入你想運行的代碼。這個也是僅運行幾行的。
退出的話,輸入
^Z
或者鍵盤:
Ctrl + z
之後回車。
method 5
F5,選擇python file即可。
斷點運行。打斷點後,同上。
去除斷點的話就可以直接運行全部了。
method 6
在上一方法中,在debug console裏面run。
斷點測試快捷鍵
A debug toolbar appears along the top with the following commands from left to right: continue (F5), step over (F10), step into (F11), step out (Shift+F11), restart (Ctrl+Shift+F5), and stop (Shift+F5).
method 7
用Django。如果沒有設置過launch.json的話,F5,然後選擇Django。輸入文件名字,即可運行。
method 8
launch配置1:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
launch配置2:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\hello.py",
"args": [
"runserver",
"--noreload"
],
"django": true
}
]
}