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:
But it did not work. How do I launch my script with arguments?
P.S. I am on Ubuntu
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:
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 holding the script name of course):
['/Path/to/current/folder/test.py', 'file1.txt', 'file2.txt']
In addition to Jim’s answer (sorry not enough rep points to make a comment), just wanted to point out that the arguments specified in PyCharm do not have special characters escaped, unlike what you would do on the command line. So, whereas on the command line you’d do:
python mediadb.py /media/paul/New\ Volume/Users/paul/Documents/spinmaster/\*.png
the PyCharm parameter would be:
For the sake of others who are wondering on how to get to this window. Here’s how:
The first parameter is the name of the script you want to run. From the second parameter onwards it is the the parameters that you want to pass from your command line. Below is a test script:
from sys import argv script, first, second = argv print "Script is ",script print "first is ",first print "second is ",second
And here is how you pass the input parameters :
‘Path to your script’,’First Parameter’,’Second Parameter’
Lets say that the Path to your script is /home/my_folder/test.py , the output will be like :
Script is /home/my_folder/test.py first is First Parameter second is Second Parameter
Hope this helps as it took me sometime to figure out input parameters are comma separated.