'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



Solution 1:[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:

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']

Solution 2:[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 enter image description here) and going to the Edit Configurations. A gif provided for clarity.

enter image description here

Solution 3:[3]

On PyCharm Community or Professional Edition 2019.1+ :

  1. From the menu bar click Run -> Edit Configurations
  2. Add your arguments in the Parameters textbox (for example file2.txt file3.txt, or --myFlag myArg --anotherFlag mySecondArg)
  3. Click Apply
  4. Click OK

Solution 4:[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:[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.

Solution 6:[6]

Add the following to the top of your Python file.

import sys

sys.argv = [
    __file__,
    'arg1',
    'arg2'
]

Now, you can simply right click on the Python script.

Solution 7:[7]

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
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

It took me some time to figure out that input parameters are comma separated.

Solution 8:[8]

I believe it's included even in Edu version. Just right click the solid green arrow button (Run) and choose "Add parameters".

Solution 9:[9]

It works in the edu version for me. It was not necessary for me to specify a -s option in the interpreter options.

Pycharm parameters for running with command line

Solution 10:[10]

In edit configuration of PyCharm when you are giving your arguments as string, you should not use '' (these quotations) for giving your input.

Instead of -s'file1.txt', -s'file2.txt' simply use:

-s file1.txt, -s file2.txt

Solution 11:[11]

you can used -argName"argValue" like -d"rd-demo" to add Pycharm arguments

 -d"rd-demo" -u"estate"

Arguments added in Parameters Section after selected edit Configuration from IDE

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3
Solution 4 Paul Miller
Solution 5 Jan Bodnar
Solution 6 Ahmed
Solution 7 bad_coder
Solution 8 Py Learner
Solution 9 Stephen Rauch
Solution 10 Marjan
Solution 11 Ahmed Saber