【问题标题】:Add keyboard shortcut to run .py script with current file as argument添加键盘快捷键以使用当前文件作为参数运行 .py 脚本
【发布时间】:2021-05-20 07:39:24
【问题描述】:

假设我在 ~/Scripts/script.py 中有一个 .py 脚本,如何使用 PyCharm 2020.3 Professional 中当前打开的文件运行它?

我只需要将键盘快捷键绑定到python ~/Scripts/script.py <current_file> 命令并在控制台中查看输出。

编辑:我想我需要一个相当于 https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner 的 PyCharm

【问题讨论】:

标签: python pycharm run-script


【解决方案1】:

最简单的方法是获取 AutoHotKey 并使用它,因为它有很多功能,而且我认为你不能在 PyCharm 中设置额外的快捷方式,两个版本。你可以在一个单独的文件中尝试这个:

import os
os.system("path/to/script.py path/to/otherfile.py")

或者如果您需要将第二个文件作为 STDIN 或输入​​:

import os
os.system("path/to/script.py<path/to/otherfile.py)

警告,第二个会将您的 otherfile.py 转换为 STDIN,因此它可以通过 input() 逐行读取 额外
AHK 脚本为您服务:

#SingleInstance, force
; # is for windows key, ! is for Alt key, ^ is for control key, + is for Shift key
^+r:: ; Will use Ctrl+Shift+R
Run, python path/to/script.py path/to/otherfile.py
return

【讨论】:

  • 这个好像没有和PyCharm集成,所以不会自动填写path/to/otherfile.py吧?
  • 我认为它可能,如果 Pycharm WINDOW 名称中包含文件名。我认为 AHK Scripting 有一个 str.replace() 函数,因此可以删除多余的位。但同样,我不是 AHK 专家。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-05
  • 2011-08-22
  • 2018-06-29
  • 2017-09-17
相关资源
最近更新 更多