Python PyCharm“运行配置”要求“脚本参数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20868780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:26:19  来源:igfitidea点击:

PyCharm "Run configuration" asking for "script parameters"

pythonidepycharm

提问by Paidhi

While writing an application parsing command line arguments I would like to run it with various parameters.

在编写解析命令行参数的应用程序时,我想使用各种参数运行它。

I don't want to create a Run Configurationfor every possible command line argument that I want my script to test with. Is there a way in PyCharm (and I guess with any JetBrains IDE) to make a Run Configurationthat asks for the Script parameterswhen executed?

我不想为我希望我的脚本测试的每个可能的命令行参数创建一个运行配置。PyCharm 中有没有办法(我猜任何 JetBrains IDE)可以创建一个运行配置,在执行时要求提供脚本参数

I am currently using PyCharm 3.1 (EAP).

我目前正在使用 PyCharm 3.1 (EAP)。

采纳答案by yole

Currently the only possibility is to use the "Before launch | Show this page" option.

目前唯一的可能是使用“启动前 | 显示此页面”选项。

回答by Yatharth Agarwal

Although yole's answeris the de facto way to be prompted for thw arguments before running a program, it is slightly annoying because:

虽然yole 的答案是在运行程序之前提示输入thw 参数的事实上的方式,但它有点烦人,因为:

  • the dialog is visually overwhelming and cluttered instead of focused on what you want to do;
  • you have to tab to reach the arguments field if you want to use the keyboard exclusively (and why not?);
  • 对话在视觉上是压倒性的和混乱的,而不是专注于你想做的事情;
  • 如果您想专门使用键盘,您必须按 Tab 键才能到达参数字段(为什么不呢?);

Nothing you could do about that. (Except maybe file a ticket. Have you done that?)

对此你无能为力。(除了可能提交票证。你这样做了吗?)



I'm just adding what I used to do before I knew aboutGoogled for this option for the sake of completeness (obvously, this is a hack in the least glamorous sense of the term). But it did suit my workflow as I often only had discrete lines to test with, and didn't switch that often.

我只是添加了我之前做过的事情 知道为了完整起见,谷歌搜索了这个选项(显然,这是一个最不迷人的术语)。但它确实适合我的工作流程,因为我经常只有离散的线路来测试,而且不经常切换。

  1. Create a new configuration set to the same file, but with a special 'magic' parameter;
  2. Add code to your script to check if the magic is there;
  3. Use a string variable instead of sys.argv(pass it through lambda args: [__name__] + args.split()to reduce the boilerplate);
  4. ???
  5. Profit;
  1. 创建一个新的配置集到同一个文件,但有一个特殊的“magic”参数;
  2. 将代码添加到您的脚本中以检查魔术是否存在;
  3. 使用字符串变量代替sys.argv(通过它lambda args: [__name__] + args.split()来减少样板文件);
  4. ???
  5. 利润;

回答by pygmy

I'm doing this on a Mac, but hopefully this will be helpful for Windows or Linux.

我正在 Mac 上执行此操作,但希望这对 Windows 或 Linux 有所帮助。

Go to Run > Edit Configurations There will be a dialog box that opens. Script: file you want to run (ending with .py) Script Parameters: the command line arguments Working Directory: directory where your project is.

转到“运行”>“编辑配置”,将打开一个对话框。脚本:要运行的文件(以 .py 结尾) 脚本参数:命令行参数 工作目录:项目所在的目录。

Hope this helps!

希望这可以帮助!

回答by BrainAtom

My simple answer is adding another wrapper as the cover in the source code which will run on the selection you made through code branch or external command or file, so choosing different branch is just a 'ddp' tap distance in vim(line change for parameter settings). You dont have to depend on pycharm updating by building your own code world:)

我的简单答案是在源代码中添加另一个包装器作为封面,它将在您通过代码分支或外部命令或文件所做的选择上运行,因此选择不同的分支只是 vim 中的“ddp”点击距离(参数的行更改)设置)。您不必通过构建自己的代码世界来依赖 pycharm 更新:)

回答by jca258

I've found today that now is possible to ask for parameters using the "Prompt" macro on the "Run configuration" parameters field.

我今天发现现在可以使用“运行配置”参数字段上的“提示”宏来请求参数。

https://www.jetbrains.com/help/pycharm/code-running-assistance-tutorial.html#parameter-with-macros

https://www.jetbrains.com/help/pycharm/code-running-assistance-tutorial.html#parameter-with-macros