17

I am using Visual Studio Code to debug my Python code.

I want to redirect one text file in stdin and the output be written in another file.

I can reach that by running python directly using the following syntax:

python code.py < input.txt > output.txt

Is there a way to allow that while debugging the script? If that is not possible, can I create a configuration for running python with that parameters. I tried using args parameter in the launch.json, but those are all placed into quotes that defeats the purpose of this redirection.

3 Answers 3

24

I was able to achieve this using the args parameter in the launch.json config file:

{
    // 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: Aktuelle Datei",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": [ "<", "input.txt",
                 ">", "output.txt" ]
        }
    ]
}

When running the debugger you get the following command line:

bash-3.2$  env DEBUGPY_LAUNCHER_PORT=51669 /usr/local/opt/python/bin/python3.7 /Users/XXXX/.vscode/extensions/ms-python.python-2020.4.76186/pythonFiles/lib/python/debugpy/wheels/debugpy/launcher "/Users/XXXX/my_script.py" < input.txt > output.txt 

Visual Studio Code 1.45
macOS 10.14.6

Sign up to request clarification or add additional context in comments.

3 Comments

Setting "console": "integratedTerminal" while redirecting the standard input throws an error: Error processing 'configurationDone' request. The handle is invalid. console property should be set to something like internalConsole.
Got the error: "The '<' operator is reserved for future use." vscode 1.69.2, windows
You wouldn't expect this to work because the redirection operator is not a program argument. However, VSCode 1.71 on Ubuntu this did actually work for me. Using C++
3

There isn't any built-in solution, but if you modified your app to replace sys.stdin and sys.stdout with what you would like to be considered stdin and stdout you could get the same effect.

Comments

1

Hi this seems work for me:

        {
            "name": "Python: 01-vector-add",
            "type": "debugpy",
            "request": "launch",
            "program": "${workspaceFolder}/triton-project/python/tutorials/01-vector-add.py",
            "console": "integratedTerminal",
            "justMyCode": false,
            "env": {
                "MLIR_ENABLE_DUMP": "1",
                "LLVM_IR_ENABLE_DUMP": "1",
                "TRITON_ENABLE_LLVM_DEBUG": "1",
                "TRITON_CACHE_DIR" : "${workspaceFolder}/.cache"
            },
            "args": "1>${workspaceFolder}/01-vector-add.ll 2>&1"
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.