Command line arguments in VSCode work fine when setup in the launch.json
file.
But when using "Python: Run Python file in Terminal" (run, not debug), the arguments in launch.json
don’t seem to appear in sys.argv
.
Is there a way to pass command line arguments to Python code when using "Python: Run Python file in Terminal"?
2
Answers
You can pass arguments via
args
inside of the JSON.Sample:
Source: VSCode
Vscode is just an editing shell, which is essentially a CMD command window. If you only use run in terminal, it will only run the file in the terminal without getting command line parameters.
You have the following alternative solutions:
Run with debug and define args in the
launch.json
file.use method
parser.add_argument()
to pass arguments in your codes. For example:import argparse
parser = argparse.ArgumentParser(description="para transfer")
parser.add_argument("--para", type=str, default="helloWorld", help="para -> str type.")
args = parser.parse_args()
print(args)
Manually run Python file in terminal and pass parameters.
python test.py --para helloworld
Try extension code-runner, you can set the custom command to run in the
settings.json
:{
}