How to run a Python program with arguments from within Visual Studio Code?
Asked Answered
A

6

35

I am running a Python program that takes some command line arguments. How can I provide these arguments when I am building a program within the Visual Studio Code?

Aguila answered 14/10, 2016 at 7:3 Comment(1)
You can put the parameters in the properties dialog of your project.Clamper
M
30

You can pass in the arguments into the program by defining the arguments in the args setting of launch.json as defined below:

json
{
    "name": "Python",
    "type": "python",
    "pythonPath":"${config.python.pythonPath}", 
    "request": "launch",
    "stopOnEntry": true,
    "console": "none",
    "program": "${file}",
    "cwd": "${workspaceRoot}",
    "args":["arg1", "arg2"],
    "env": {"name":"value"}
}

Further information can be found on the documentation site here: https://github.com/DonJayamanne/pythonVSCode/wiki/Debugging#args

Mulvihill answered 14/10, 2016 at 11:15 Comment(7)
This works, but in the debug mode. Is it possible to make it run in the non-debug mode?Aguila
@Aguila you ever find an answer for this?Minuend
Not the right answer: This only works for debugging (as cerebrou already pointed out)Rhesus
You can use Run without debugging option, it'll still use the same options and it will not debug your codeMulvihill
@CoryKramer can you give a hint on how to add a console argument for python executable itself (not the program)? I just need to run "python -i <scriptfile>" so I can continue in the integrated console. I am struggling with the docs and forums for just a simple thing like that and I can't find it.Midpoint
@don you have the actual answer, do you want to add an answer so it is more visible?Blight
Vscode said "pythonPath" is not valid key, but I just removed this key and everything worked for me. ThanksAvner
B
3

If you use the Code Runner extension you can add the following to your settings (click on the '{}' icon in the top right corner to get the settings.json file):

"code-runner.executorMap": { "python": "$pythonPath -u $fullFileName xxx" }

where xxx is your argument. This is a global change so you have to change when working on other files.

Baber answered 18/5, 2019 at 13:37 Comment(1)
A better method would be to just keep it simple and run the command manually from the terminal and supply the necessary command line arguments. Or better yet, use a Makefile with custom targets. I find code runner works best for running programs without command-line arguments.Distiller
A
0

One way to do it in the version 2.0.0 is:

"command": "python ${file} --model_type LeNet5 --prior_file conf1.json --epochs 200",
Aguila answered 29/3, 2019 at 14:16 Comment(1)
where do you add this? launch.json is for debugging, isn'tRhesus
T
0

running your script from the command line in the terminal works.

Tanto answered 3/8, 2022 at 16:0 Comment(1)
but then you can not debug it. or how would you do it?Chopine
A
0

According to vscode site here

it is adding similar

"args" : ["--port", "1593"]

in launch.json

Astrea answered 12/12, 2022 at 0:32 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Bilabiate
I
0

I was also looking for an answer for this. Setting in launch.json works, but you can only use debugging afterwards and if you have more that one configuration, it makes it hard to go back and forth.

So I write a simple function that I can load in main app file. You can give your args with a json file with this method and it works for both debugging and normally running script

import sys
import json    

def loadArgs(path):
    with open(path) as args_file:
        args = json.load(args_file)
    

    for arg in args:
        name = '--' + arg
        value = args[arg]

        if name not in sys.argv:
            sys.argv.append(name)
            sys.argv.append(value)

    print('Args are loaded...')

And import in your main file

from set_args import loadArgs

loadArgs(path='args.json')
Illimani answered 18/6, 2023 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.