argparse 调试

发布于 2025-01-11 11:30:50 字数 657 浏览 0 评论 0原文

为了简化起见,这里是调试失败的代码。有两个问题:

  1. 我似乎无法创建允许在调试或非调试模式下传递参数的运行配置。

  2. 此代码在调试模式下总是失败?

    导入argparse
    解析器 = argparse.ArgumentParser()
    parser.add_argument('文件名', type=argparse.FileType('r'))
    args = parser.parse_args()
    print("文件名:", args.文件名)
    

以下是运行的示例配置:

        {
            "name": "Python: filetest.py",
            "type": "python",
            "request": "launch",
            "program": 
            "${workspaceFolder}/filetest.py",
            "args": [
                "./samples/blja2.wav"
            ],
            "console": "integratedTerminal"
        }

To simplify, here is code that fails in debug. There are two problems:

  1. I cannot seem to create a run configuration that allows for passing arguments in either debug or non-debug mode.

  2. This code always fails in debug mode?

    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('filename', type=argparse.FileType('r'))
    args = parser.parse_args()
    print("Filename: ", args.filename)
    

Here is a sample configuration for Run:

        {
            "name": "Python: filetest.py",
            "type": "python",
            "request": "launch",
            "program": 
            "${workspaceFolder}/filetest.py",
            "args": [
                "./samples/blja2.wav"
            ],
            "console": "integratedTerminal"
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷弦 2025-01-18 11:30:50
import argparse


def argparse_sample():
    parser = argparse.ArgumentParser()
    parser.add_argument('sample_args', type=argparse.FileType('r'))
    args = parser.parse_args()
    print("File Content: ", args.sample_args.readlines())


if __name__ == '__main__':
    argparse_sample()

所以没有调试模式:
输出:

$ python main.py sample_args.txt
Filename:  ['        {\n', '            "name": "Python: filetest.py",\n', '            "type": "python",\n', '
   "request": "launch",\n', '            "program": \n', '            "${workspaceFolder}/filetest.py",\n', '
 "args": [\n', '                "./samples/blja2.wav"\n', '            ],\n', '            "console": "integratedTermina
l"\n', '        }']

在 PyCharm 中使用调试模式:
PyCharm 调试设置

输出:

C:\projects\StackOverFlow\venv\Scripts\python.exe C:/projects/StackOverFlow/main.py sample_args.txt
Filename:  ['        {\n', '            "name": "Python: filetest.py",\n', '            "type": "python",\n', '            "request": "launch",\n', '            "program": \n', '            "${workspaceFolder}/filetest.py",\n', '            "args": [\n', '                "./samples/blja2.wav"\n', '            ],\n', '            "console": "integratedTerminal"\n', '        }']

Process finished with exit code 0

Visual Studio Code 调试器设置 (launch.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": [
        {   "args": ["sample_args.txt"],
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

输出:

PS C:\projects\StackOverFlow>  c:; cd 'c:\projects\StackOverFlow'; & 'C:\Python38\python.exe' 'c:\Users\alist\.vscode\extensions\ms-python.python-2021.8.1159798656\pythonFiles\lib\python\debugpy\launcher' '49710' '--' 'c:\projects\StackOverFlow\main.py' 'sample_args.txt'
Filename:  ['        {\n', '            "name": "Python: filetest.py",\n', '            "type": "python",\n', '            "request": "launch",\n', '            "program": \n', '            "${workspaceFolder}/filetest.py",\n', '            "args": [\n', '                "./samples/blja2.wav"\n', '            ],\n', '            "console": "integratedTerminal"\n', '        }']
import argparse


def argparse_sample():
    parser = argparse.ArgumentParser()
    parser.add_argument('sample_args', type=argparse.FileType('r'))
    args = parser.parse_args()
    print("File Content: ", args.sample_args.readlines())


if __name__ == '__main__':
    argparse_sample()

So without the debug mode:
Output:

$ python main.py sample_args.txt
Filename:  ['        {\n', '            "name": "Python: filetest.py",\n', '            "type": "python",\n', '
   "request": "launch",\n', '            "program": \n', '            "${workspaceFolder}/filetest.py",\n', '
 "args": [\n', '                "./samples/blja2.wav"\n', '            ],\n', '            "console": "integratedTermina
l"\n', '        }']

With debug mode in PyCharm:
PyCharm debug settings

Output:

C:\projects\StackOverFlow\venv\Scripts\python.exe C:/projects/StackOverFlow/main.py sample_args.txt
Filename:  ['        {\n', '            "name": "Python: filetest.py",\n', '            "type": "python",\n', '            "request": "launch",\n', '            "program": \n', '            "${workspaceFolder}/filetest.py",\n', '            "args": [\n', '                "./samples/blja2.wav"\n', '            ],\n', '            "console": "integratedTerminal"\n', '        }']

Process finished with exit code 0

Visual Studio Code Debugger settings (launch.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": [
        {   "args": ["sample_args.txt"],
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

Output:

PS C:\projects\StackOverFlow>  c:; cd 'c:\projects\StackOverFlow'; & 'C:\Python38\python.exe' 'c:\Users\alist\.vscode\extensions\ms-python.python-2021.8.1159798656\pythonFiles\lib\python\debugpy\launcher' '49710' '--' 'c:\projects\StackOverFlow\main.py' 'sample_args.txt'
Filename:  ['        {\n', '            "name": "Python: filetest.py",\n', '            "type": "python",\n', '            "request": "launch",\n', '            "program": \n', '            "${workspaceFolder}/filetest.py",\n', '            "args": [\n', '                "./samples/blja2.wav"\n', '            ],\n', '            "console": "integratedTerminal"\n', '        }']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文