介绍
这里小虎介绍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
}
]
}