当前位置:   article > 正文

VSCode-Python传参数进行Debug_vscode如何传参数

vscode如何传参数

新建demo.py

  1. import argparse
  2. def parse_args():
  3. description = "debug example"
  4. parser = argparse.ArgumentParser(description=description)
  5. help = "The path of address"
  6. parser.add_argument('--host',help = help)
  7. parser.add_argument('--port',help = help)
  8. parser.add_argument('--user',help = help)
  9. args = parser.parse_args()
  10. return args
  11. if __name__ == '__main__':
  12. args = parse_args()
  13. host = args.host
  14. port = args.port
  15. user = args.user
  16. print([host, port, user])
  17. '''
  18. python demo.py --host 127.0.0.1 --port 22 --user root
  19. '''

命令行运行 python demo.py --host 127.0.0.1 --port 22 --user root 可以看到输出结果

在vscode点击debug的图标-->create a launch.json file--->python File

初始的json文件如下:

  1. {
  2. // Use IntelliSense to learn about possible attributes.
  3. // Hover to view descriptions of existing attributes.
  4. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "Python: Current File",
  9. "type": "python",
  10. "request": "launch",
  11. "program": "${file}",
  12. "console": "integratedTerminal",
  13. "justMyCode": true
  14. }
  15. ]
  16. }

Ctrl+shif+p切换python环境并更改./vscode/launch.json如下

  1. {
  2. // Use IntelliSense to learn about possible attributes.
  3. // Hover to view descriptions of existing attributes.
  4. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "Python: Current File",
  9. "type": "python",
  10. "request": "launch",
  11. "program": "${file}",
  12. "console": "integratedTerminal",
  13. "justMyCode": true,
  14. "env": {
  15. "CUDA_VISIBLE_DEVICES":"4" // 设置cuda
  16. },
  17. // 添加参数
  18. "args":[
  19. "--host", "127.0.0.1",
  20. "--port", "22",
  21. "--user","root"
  22. ]
  23. }
  24. ]
  25. }

之后打断点按F5或者Run-->Start Debugging 就可以了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/770921
推荐阅读
相关标签
  

闽ICP备14008679号