当我从 Visual Studio 代码中点击运行按钮(或 F5)时,我想自动启动 Python http.server。 我认为这是 launch.json 配置的问题,但我不熟悉它。 我怎样才能做到这一点?
最佳答案
请安装 pythonVSCode 扩展。并在项目目录中创建 python 文件。并将以下内容放在上面。请参阅here
import http.server
import socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
并像这样创建启动配置...
{
"name": "Documentation",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${workspaceRoot}/env/bin/python",
"program": "${workspaceRoot}/your_pyton_run_file.py",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
},
然后你可以按 F5 启动
关于visual-studio-code - 从 Visual Studio Code 自动启动 Python Web 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40179185/