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

查看:38
本文介绍了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?

附言我在 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天全站免登陆