How to Use Argv with Spyder

How to parse arguments in python (spyder)?

You can parse arguments by doing a special run from the settings and putting in the order that the arguments are expected.

how to execute Python 3.3 script in Spyder console with variables?

You need to go

Run > Configuration per file

(or hit Ctrl+F6) and in the dialog that it appears you need to check

Command line options

and write (for example) there

1 2 3

After closing this dialog and hitting F5, you'll see the output you are expecting.

Note: Please remember that these command line options are saved between Spyder restarts as part of the file run config, so if you want to change them, you need to hit Ctrl+F6 again.

Spyder command line

In Spyder, if you go into the Run menu, you'll find an option named Configuration per file. On my system, you can get directly to that entry with CTRL+F6 (the shortcut key may be different on other OSs).

Once you select the menu item, you'll get a dialog window with several options. The one you want is about halfway down, named Command line options. You will want to check the box, and put the arguments you showed above into the adjacent text field. Now when you run the file, the code will behave as if those options were passed to it on the command line (it will see them in sys.argv).

Passing Arguments to Spyder for debugging file

Go to run > configure

Tick command line options and type in the arguments in the space.

Distinguish runs from Command Prompt vs Spyder console

If you import sys in the console and then call sys.argv, it will show you the value ['']. While running a script within Spyder expands that array to ['script1.py'] (plus the file address), it will still not get larger than one entry.

If, on the other hand, you run the script from the command line the way you showed above, then sys.argv will have a value of ['script1.py', 'dataFile.csv', 'Results', 'outputFile.csv']. You can utilize the differences between these to distinguish between the cases.

What are the best differences to use? You want to distinguish between two possibilities, so an if - else pair would be best in the code. What's true in one case and false in the other? if sys.argv will not work, because in both cases the list contains at least one string, so that will be effectively True in both cases.

if len(sys.argv) > 1 works, and it adds the capability to run from the command line and go with what is coded for the console case.



Related Topics



Leave a reply



Submit