ChatGPT解决这个技术问题 Extra ChatGPT

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


D
Dimitris Fasarakis Hilliard

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:

https://i.stack.imgur.com/HOh9C.png

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

Thanks it worked. In addition it turns out that I should have used "" quotes instead of ''.
This 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.
I think this answer is out of date because there is no script parameters
@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.
answer is outdated. for pycharm 2019.1.3 the field is just called Parametersand you don't have to put double quotes around your parameters.
t
tisaconundrum

For the sake of others who are wondering on how to get to this window. Here's how:

https://i.stack.imgur.com/WG6Jo.png

https://i.stack.imgur.com/6KQF2.gif


The 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.
This was more helpful than the best best upvoted one. Thanks!
G
Giorgos Myrianthous

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


P
Paul Miller

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"

J
Jan Bodnar

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.


A
Ahmed

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.


b
bad_coder

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.


P
Py Learner

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


S
Stephen Rauch

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

https://i.stack.imgur.com/ZnkVi.png


M
Marjan

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


A
Ahmed Saber

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


m
matan antebi

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:

bar menu Run-->Edit Configuration Define the Name for your debug/run script. 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