Is there an easy way to run a Python file inside Visual Studio Code with arguments?
I know I can add a custom configuration in the launch.json
file with the
Another option is you can run your python program from the commandline via
python3 -m debugpy --wait-for-client --listen localhost:5678 myprogram.py
Then you can use a Python: Remote Attach
launch.json configuration to connect to the program. It means an extra step every time to you turn on your program: run the program on the CLI then attach debugger, but it does make specifying the arguments more fluid.
To make things even simpler, you could put the above in a bash script and pass through args from CLI to python:
start.sh "my arg that changes all the time"
Then attach via "Remote Attach".
If you are using a virtual environment, be sure to use the full path to the environment's Python interpreter.
Maybe this will do what you want: create a python.bat file in your %PATH% that points to python.exe:
C:\Users\joecoder\AppData\Local\Programs\Python\Python37\python.exe %*
Then use a Visual Studio Code terminal window (you can open a new one from the Terminal tab at the top of Visual Studio Code) to change directory to where your .py file resides, and run as normal:
PS C:\Users\joecoder> cd vscode\python
PS C:\Users\joecoder\vscode\python> python test.py 1 2 3
Of course this runs outside of Visual Studio Code, so be sure to write out changes after edits and you'll have to use print() style debugging.
A workaround is to have your script ask for the command-line arguments (in the internal Visual Studio Code console).
This can be made much more usable by leaning on readline, which allows you to do things like press the Up arrow key to cycle through previous commands (command history), and more. An example:
import argparse, readline
def main():
# Ask for additional command line arguments if needed (for VSCode)
parser = argparse.ArgumentParser()
parser.add_argument('--interactive', action='store_true', default=False)
(args, rest) = parser.parse_known_args()
if args.interactive:
try: readline.read_history_file()
except: pass
rest += input("Arguments: ").split(" ") # Get input args
try: readline.write_history_file()
except: pass
# Your other script arguments go here
parser.add_argument("-output-dir", default="/out")
# ...
args = parser.parse_args(rest)
print(args)
if __name__ == "__main__":
main()
Then just set up Visual Studio Code to always pass in the --interactive
argument, and your script will always ask for arguments (with history!) even as you set breakpoints.
If you don’t have a task.json file in your project you can create a new one with press Ctrl + Shift + B. Then choose the first option showing to you then replace all of them with the below:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run Python with argument",
"type": "shell",
"command": "python PROGRAM_NAME.py ARG1 ARG2 ...",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Otherwise add the above configuration in your existed tasks.json file.
Replace PROGRAM_NAME in the above configuration with your program name and ARG1 ARG2 ... indicate your specific arguments.
After all, you can execute your created task with Ctrl + Shift + B and choose the new "Run Python with argument" task.
Visual Studio Code only supports one launch.json file. However, it supports two or more configurations, and they appear in the left-hand menu/pane's drop down list (instead of "No Configurations").
Here are the instructions. Click the Config button in the DEBUG pane circled in red above:
Click it and it creates a launch.json file with debugging configurations. Edit this file and add the args in this key-pair format AND add multiple for different args including Variable Substitution!
{
// 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 (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"args": [
"--username", "Jeremy",
"--account", "Stackoverflow"
],
"console": "integratedTerminal"
},
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"args": ["${env:USERNAME}"],
"console": "integratedTerminal"
}
]
}
Put a breakpoint in your Python script, for example on the first line under def main(...)
and then press F5 or click Start Debugging.