Pycharm 和 sys.argv 参数

我试图调试一个脚本,其中采取命令行参数作为输入。参数是同一目录中的文本文件。Script 从 sys.argv 列表获取文件名。我的问题是,我不能用魅力来启动脚本。

我尝试在“运行”> “编辑配置”菜单中的“脚本参数”字段中输入参数,如下所示:

-s'file1.txt', -s'file2.txt'

但是它没有工作。我如何启动我的脚本与参数?

另外,我在用 Ubuntu

172320 次浏览

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"

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

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.

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.

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

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

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.

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

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

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

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