如何在VisualStudio代码中运行需要外部数据的Python文件

2024-05-18 02:45:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在VSC中运行Python文件,但该文件需要来自两个不同.txt文件的外部数据

例如,如果我在Python空闲模式下运行它,我会打开Python文件,选择run…Customized,然后输入.txt文件的路径(例如data/xxxxx.txt data/yyyy.txt output.png),然后运行它

能否请您指出,在VSC中是否有这样的方式运行文件,或者请告诉我在哪里可以找到任何支持此特殊要求的文档

这是发生异常的函数:

def main():

    # Check usage
    if len(sys.argv) not in [3, 4]:
        sys.exit("Usage: python generate.py structure words [output]")

    # Parse command-line arguments
    structure = sys.argv[1]
    words = sys.argv[2]
    output = sys.argv[3] if len(sys.argv) == 4 else None

    # Generate crossword
    crossword = Crossword(structure, words)
    creator = CrosswordCreator(crossword)
    assignment = creator.solve()

    # Print result
    if assignment is None:
        print("No solution.")
    else:
        creator.print(assignment)
        if output:
            creator.save(assignment, output)

这是VSC中的一个例外:

Exception has occurred: SystemExit

Usage: python myfile.py structure words [output]

  File "/Users/#######/myfile.py", line 277, in main
    sys.exit("Usage: python myfile.py structure words [output]")   
 File "/Users//#######/myfile.py", line 299, in <module>
   main()

Tags: 文件inpytxtoutputifmainsys
2条回答

似乎您希望使用VS代码调试器运行python文件,但无法告诉它启动程序所使用的参数

导航到debugger选项卡(Ctrl/Cmd+Shift+D)并单击“创建一个launch.json文件”,在出现的提示中指定要向python文件添加启动配置

Screenshot to create a launch.json file

它应该创建并打开一个样板json文件。在"configurations"的第一个元素中,您可以添加一个键"args",其中包含数组中所有必需的参数

完整地说,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",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",

            // this is what I added manually
            "args": [
                "a.txt",
                "b.txt",
                "out.png"
            ]
        }
    ]
}

然后,打开python文件后,通过debug选项卡或右上角的绿色箭头启动它

编辑:

通过频繁更改命令行参数,您可以告诉VS代码prompt您这些参数。请参阅下面的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",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": [
                "${input:firstArg}",
                "${input:secondArg}",
                "out.png"
            ]
        }
    ],

    "inputs": [
        {
            "id": "firstArg",
            "type": "promptString",
            "default": "myDefault.txt",
            "description": "First txt"
        },
        {
            "id": "secondArg",
            "type": "promptString",
            "description": "Now your second txt"
        }
    ]
}

您需要为VSC单独安装python,因为它是一个扩展

安装后:

python filename.py

有关详细信息:VS code documentation

相关问题 更多 >