Visual Studio Code: run python file with arguments

visual studio code matplotlib
how to run code in visual studio code
ptvsd
vscode-python debugger not working
visual studio code python
python for visual studio
how to run python script in vscode
tkinter visual studio code

Is there any easy way to run a python file inside the VS Code with arguments?

I know I can add a custom configuration in the launch.json file with the args keyword. However it is annoying to modify the launch.json every time just because I want to use different arguments.

You can add a custom task to do this. This deals with the tasks.json. You can add a default tasks.json file for you project (project folder). Follow these steps. Keyboard press Ctrl+Shift+B It will prompt the following popup

Click on the Configure Build Task If there is already a custom tasks.json created in the following location .vscode/tasks.json editor will open it, If not it will give a drop down of suggestions of already existing task runners.

Our Intention is to create a custom tasks.json for our project so to create one we need to select Others option from the drop down. Check the screenshot below.

Once you select Others option you could see a default tasks.json will get created from the root directory of the project to the following location .vscode/tasks.json Below is an example of tasks.json

now edit the tasks.json to support Python.

  1. Change the Command property from "echo" to "Python"
  2. Keep showOutput as "Always"
  3. Change args (Arguments) from ["Hello World"] to ["${file}"] (filename)
  4. Delete the last property problemMatcher

  5. Keep isShellCommand and version properties as unchanged

  6. Save the changes made

You can now open your py file and run it nicely with the shortcut Ctrl+Shift+B.

Debugging configurations for Python apps in Visual Studio Code, Alternately, you can run VS Code through the operating system UI, then use File > Open Folder to open the  Run the code you've written to this point by pressing Ctrl+F5 or selecting Debug > Start without Debugging menu item. Visual Studio warns you if you still have errors in your code. When you run the program, a console window appears displaying the results, just as if you'd run a Python interpreter with PythonApplication1.py from the command line. Press a key to close the window and return to the Visual Studio editor.

A workaround is to have your script ask for the command-line arguments (in the internal VSCode 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 VSCode to always pass in the --interactive argument, and your script will always ask for arguments (with history!) even as you set breakpoints.

Get Started Tutorial for Python in Visual Studio Code, In the text editor: right-click anywhere in the editor and select Run Python File in Each framework also has specific settings, such as arguments that identify  Note all the settings files for workspace are stored in a .vscode folder in the workspace folder. If you have a program to run, load it here. At this point, you can run the program with ctrl-F5 or start debugging with F5. (One thing to note with Visual Studio Code: Whenever you need to run or debug a file, make sure it is the current open one.

You would have to programmatically change the file or have another config file with different arguments you copy/paste.


Here's the instructions for everyone else, click the Config button in the DEBUG Pane:

Click it and it creates a launch.json file with debugging configurations. Edit this file and add the args in this key-pair format:

{
    // 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"
        },
        {

Put a breakpoint in your Python script, for example on the first line under def main(...) and then press F5 or click Start Debugging.

Python in Visual Studio Code, Once discovered, Visual Studio Code provides a variety of means to run tests and Unit tests are concerned only with the unit's interface—its arguments and return Python tests are Python classes that reside in separate files from the code  and run Preferences: Open Settings (JSON)) and set the “ python.experiments.enabled ” setting to false. Be sure to download the Python extension for Visual Studio Code now to try out the above improvements. If you run into any problems or have suggestions, please file an issue on the Python VS Code GitHub page.

If you dont 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 them with 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 above configuration in your existed tasks.json.

Replace PROGRAM_NAME in 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 new "run python with argument" task.

Testing Python in Visual Studio Code, Hi, I've just started using VS Code for python scripting. It looks rather complete so far. One thing I can't yet figure out is how to add command line arguments to a script I'm editing. It seems that Launch.json is where you add args. That's less  Python in Visual Studio supports debugging without a project. With a stand-alone Python file open, right-click in the editor, select Start with Debugging, and Visual Studio launches the script with the global default environment (see Python environments) and no arguments. But from then on, you have full debugging support.

If you are using a virtual environment, be sure to use the full path to the environment's python.

Passing command line arguments to python scripts : vscode, py scripts in the file explorer on the left. First, you want to add the Python extension from Don Jayamanne, to your VS Code environment. Hit Ctrl-  There are three other ways you can run Python code within VS Code: Right-click anywhere in the editor window and select Run Python File in Terminal (which saves the file automatically): Select one or more lines, then press Shift+Enter or right-click and select Run Selection/Line in Python Terminal. This command is convenient for testing just a part of a file.

Running Python scripts from Visual Studio Code, In Visual Studio Code, you can open an integrated terminal, initially starting at However, if you want to run a python file with parameters, it is better to invoke an​  Python debug configurations in Visual Studio Code. The Python extension supports debugging of a number of types of Python applications. For a short walkthrough of basic debugging, see Tutorial - Configure and run the debugger. Also see the Flask tutorial. Both tutorials demonstrate core skills like setting breakpoints and stepping through code.

Run python in VS code, Visual Studio provide rich debugging for Python code, including setting To control the environment and arguments, create a project for the code, which is These actions launch your project's startup file (shown in bold in  IronPython can use the .NET Framework and Python libraries, and other .NET languages can use Python code just as easily. The System.Diagnostics namespace provides classes that allow you to interact with system processes, event logs, and performance counters. Using Iron Python. Step 1: Create a project in Visual Studio

Debug Python code, The recommended value for this setting is ${file}. Resulting in the debugging of the active file in the editor. Entering the name of the Python file is also supported. However you need to ensure the file name is fully qualitified path. You have two options: Option 1: Provide the fully qualified path as follows

Comments
  • Thanks for the answer. However I don't get it.. Ctrl+Shift+B runs the script but how I can pass arguments to it?
  • I want to start the debugger for example from the command palette with something like debug arg1 arg2, which would start my script like python app.py arg1 arg2.
  • Hilarious. Down votes (I'm guessing) are because I went outside VCS to solve this? My argument: if the tool doens't do what you want in a reasonable way, definitely go outside the box. (feel free to EXPLAIN your downvote below :)