Pycharm and sys.argv arguments
Solution 1
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:
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']
Solution 2
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 ) and going to the
Edit Configurations
. A
gif provided for clarity.
Solution 3
On PyCharm Community or Professional Edition 2019.1+ :
- From the menu bar click Run -> Edit Configurations
- Add your arguments in the Parameters textbox (for example
file2.txt file3.txt
, or--myFlag myArg --anotherFlag mySecondArg
) - Click Apply
- Click OK
Solution 4
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:
"/media/paul/New Volume/Users/paul/Documents/spinmaster/*.png"
Solution 5
Notice that for some unknown reason, it is not possible to add command line arguments in the PyCharm Edu version. It can be only done in Professional and Community editions.
Related videos on Youtube
YKY
Updated on April 17, 2022Comments
-
YKY about 2 years
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
-
YKY over 8 yearsThanks it worked. In addition it turns out that I should have used "" quotes instead of ''.
-
harry over 7 yearsThis was helpful; but to clarify, my python script takes two file names as parameters, one for -r and one for -s (or both or one or neither). So my script parameters were specified as [-p "prof_samples.txt" -r "resp_samples.txt"] with no interpreter options.
-
tisaconundrum almost 7 yearsI think this answer is out of date because there is no
script parameters
-
Dimitris Fasarakis Hilliard almost 7 years@tisaconundrum after checking with this, could you tell me what version of PyCharm you are running? I just downloaded the latest (PyCharm Community Edition 2017.2.3 Build #PC-172.3968.37, built on September 1, 2017) and still see the
Script parameters
option. -
Shanemeister about 6 yearsThe short clip was actually helpful -- thank you! That's what I hate about gui things. Its like an Easter egg hunt whereas command line you just type it in.
-
Praveen L over 5 yearsAfter adding the parameter values, clicking APPLY or OK button not running the code. If I save the configurations and run the code, still arguments not considered in Pycharm python code
-
bucky about 5 yearsanswer is outdated. for pycharm 2019.1.3 the field is just called
Parameters
and you don't have to put double quotes around your parameters. -
Aakash Basu about 4 yearsThis was more helpful than the best best upvoted one. Thanks!