Pycharm and sys.argv arguments

Question:

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

Asked By: YKY

||

Answers:

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

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"
Answered By: Paul Miller

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

Answered By: tisaconundrum

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.

Answered By: Rumela Saraswati

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.

Answered By: Jan Bodnar

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

Answered By: Py Learner

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

Answered By: Awaa

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.

Answered By: Ahmed

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
Answered By: Giorgos Myrianthous

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

Answered By: Marjan

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

Answered By: Ahmed Saber

I’m using argparse, and in order to debug my scripts I also using Edit Configuration. For example below the scripts gets 3 parameters (Path, Set1, N) and an optional parameter (flag):
‘Path’ and ‘Set1’ from type str.
‘N’ from type int.
The optional parameter ‘flag’ from type boolean.

impor argparse


parser = argparse.ArgumentParser(prog='main.py')
parser.add_argument("Path", metavar="path", type=str)
parser.add_argument("Set1", type=str, help="The dataset's name.")
parser.add_argument("N", type=int, help="Number of images.")
parser.add_argument("--flag", action='store_true')
params = parser.parse_args()

In order to to run this in a debug or not by using command line, all needed is:

  1. bar menu Run–>Edit Configuration

  2. Define the Name for your debug/run script.

  3. Set the parameters section. For the above example enter as follow:

    The defualt 3 parameters must me included –> "c:mypath" "name" 50

    For the optional parameter –> "c:mypath" "name" 50 "–flag"

parameter section

Answered By: matan antebi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.