Pycharm和sys.argv参数 [英] Pycharm and sys.argv arguments

查看:322
本文介绍了Pycharm和sys.argv参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试将命令行参数作为输入的脚本.参数是同一目录中的文本文件.脚本从sys.argv列表中获取文件名.我的问题是我无法使用pycharm中的参数启动脚本.

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.我在Ubuntu上

P.S. I am on Ubuntu

推荐答案

在PyCharm中,像您一样将参数添加到 Script Parameters 中,但它们用双引号引起来 ""且未指定像-s这样的解释器标志.这些标志在 Interpreter options 框中指定.

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.

脚本参数"框内容:

"file1.txt" "file2.txt"

Interpeter标志:

Interpeter flags:

-s

或者,在视觉上:

然后,使用一个简单的测试文件进行评估:

Then, with a simple test file to evaluate:

if __name__ == "__main__":
    import sys
    print(sys.argv)

我们获得了提供的参数(其中sys.argv[0]保留了脚本名称):

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

这篇关于Pycharm和sys.argv参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆