简体   繁体   English

AutoKey Python - 如何创建全局函数库

[英]AutoKey Python - How do I create a global function library

I'm using AutoKey (not AutoHotKey), and I'm noticing that multiple scripts are using the same python functions I've created. 我正在使用AutoKey (不是AutoHotKey),我注意到多个脚本正在使用我创建的相同python函数。 Up until now, I've just been defining the same function in each script that uses it. 到目前为止,我刚刚在每个使用它的脚本中定义了相同的函数。 However, if I have to go back and modify that function, I have to remember each script that uses, and make those modifications for each. 但是,如果我必须返回并修改该功能,我必须记住每个使用的脚本,并为每个脚本进行修改。

Using AutoKey, how would I have (custom) global function library, that I'd be able to import into each script, so I'll have a centralized place for propagating function modifications to all scripts? 使用AutoKey,我将如何拥有(自定义)全局函数库,我可以将其导入到每个脚本中,因此我将有一个集中的位置来向所有脚本传播函数修改?

I've just begun using python (because AutoKey uses it for its scripting engine), so I'm not sure how I'd create my own namespace and I'm also not sure how I'd import a custom library into my AutoKey python scripts. 我刚刚开始使用python(因为AutoKey将它用于它的脚本引擎),所以我不确定我是如何创建自己的命名空间的,我也不确定如何将自定义库导入我的AutoKey python脚本。 Where would I place the code-file? 我在哪里放置代码文件? What would the syntax be inside the file that will be encapsulating my function definitions? 将封装我的函数定义的文件中的语法是什么?

You could write a module and put it in the Lib\\site-packages folder of Python. 您可以编写一个模块并将其放在Python的Lib\\site-packages文件夹中。

For example, with this: 例如,有了这个:

def foo():
    print("foo called")

stored as C:\\Python32\\Lib\\site-packages\\foo.py : 存储为C:\\Python32\\Lib\\site-packages\\foo.py

>>> import foo
>>> foo.foo()
foo called

使用最近的AutoKey版本,您可以将“脚本引擎”选项卡中的首选项“用户模块文件夹”设置为您喜欢的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM