首先是创建一个空的文件夹(比如文件夹为test),然后在其中新建一个.py文件(比如文件为hello.py),接着在vscode中打开test文件夹作为工作目录,首先需要配置一些python解释器的路径:commond+,进入设置界面,输入[python.pythonPath],然后讲路径修改为自定义的Python路径,比如[/usr/local/anaconda3/bin/python](我是使用的anaconda),到此为止,我们已经可以运行python程序了,但是如果需要调制Python程序的话,还需要接下来的一步配置:
接着点击齿轮符号,选择python file,会自动在.vscode目录下生成一个luanch.json的文件
下面给出我的文件示例:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"stopOnEntry": true
},
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "enter-your-module-name-here",
"console": "integratedTerminal"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
},
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
}
]
}
如果需要调试,那就按F5,进入调试模式即可
好了,这样就可以顺利地在vscode上进行Python发啦,赶快行动起来吧~