Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm and sys.argv arguments

I am trying to debug a script which takes command line arguments as an input. Arguments are text files in the same directory. Script gets file names from sys.argv list. My problem is I cannot launch the script with arguments in pycharm.

I have tried to enter arguments into "Script parameters" field in "Run" > "Edit configuration" menu like so:

-s'file1.txt', -s'file2.txt' 

But it did not work. How do I launch my script with arguments?

P.S. I am on Ubuntu

like image 739
YKY Avatar asked Oct 13 '15 12:10

YKY


People also ask

What is command line arguments for SYS argv in Python?

sys. argv is the list of commandline arguments passed to the Python program. argv represents all the items that come along via the command line input, it's basically an array holding the command line arguments of our program. Don't forget that the counting starts at zero (0) not one (1).

How do I grab command line arguments?

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.

Can Python take command line arguments?

Python exposes a mechanism to capture and extract your Python command line arguments. These values can be used to modify the behavior of a program. For example, if your program processes data read from a file, then you can pass the name of the file to your program, rather than hard-coding the value in your source code.

How do I run a Python script from command line arguments?

Using Python3 Command Line Arguments You can use the command line arguments by using the sys. argv[] array. The first index of the array consists of the python script file name. And from the second position, you'll have the command line arguments passed while running the python script.


2 Answers

For the sake of others who are wondering on how to get to this window. Here's how:

You can access this by clicking on Select Run/Debug Configurations (to the left of enter image description here) and going to the Edit Configurations. A gif provided for clarity.

enter image description here

like image 33
tisaconundrum Avatar answered Sep 29 '22 11:09

tisaconundrum


In PyCharm the parameters are added in the Script Parameters as you did but, they are enclosed in double quotes "" and without specifying the Interpreter flags like -s. Those flags are specified in the Interpreter options box.

Script Parameters box contents:

"file1.txt" "file2.txt" 

Interpeter flags:

-s 

Or, visually:

enter image description here

Then, with a simple test file to evaluate:

if __name__ == "__main__":     import sys     print(sys.argv) 

We get the parameters we provided (with sys.argv[0] holding the script name of course):

['/Path/to/current/folder/test.py', 'file1.txt', 'file2.txt'] 
like image 176
Dimitris Fasarakis Hilliard Avatar answered Sep 29 '22 12:09

Dimitris Fasarakis Hilliard