Pycharm and sys.argv arguments

PythonLinuxPycharm

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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

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

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

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

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

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

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

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

Solution 9 - Python

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

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYKYView Question on Stackoverflow
Solution 1 - PythonDimitris Fasarakis HilliardView Answer on Stackoverflow
Solution 2 - PythontisaconundrumView Answer on Stackoverflow
Solution 3 - PythonGiorgos MyrianthousView Answer on Stackoverflow
Solution 4 - PythonPaul MillerView Answer on Stackoverflow
Solution 5 - PythonJan BodnarView Answer on Stackoverflow
Solution 6 - PythonRumela SaraswatiView Answer on Stackoverflow
Solution 7 - PythonAhmedView Answer on Stackoverflow
Solution 8 - PythonPy LearnerView Answer on Stackoverflow
Solution 9 - PythonAwaaView Answer on Stackoverflow
Solution 10 - PythonMarjanView Answer on Stackoverflow
Solution 11 - PythonAhmed SaberView Answer on Stackoverflow