[英]Python plugin structure - execute code from another file
Let's say I have a folder containing the main-file main.py
and a configuration-file config.json
. 假设我有一个包含主文件
main.py
和配置文件config.json
的文件夹。
In the JSON-file there are multiple entries containing different filenames and the name of a method within each file. 在JSON文件中,有多个条目,每个条目包含不同的文件名和方法名称。
Depending on user input from main.py
I get the corresponding filename and the method from config.json
and now I want to execute that method. 根据
main.py
用户输入,我从config.json
获取相应的文件名和方法,现在我想执行该方法。
I tried the following 我尝试了以下
file = reports [id]["filename"]
method = reports [id]["method"]
# which would give me: file = 'test.py' and method = 'execute'
import file
file.method()
This didn't work obviously. 这显然行不通。 The problem is that I can't know which files there are during compilation.
问题是在编译过程中我不知道有哪些文件。
Other developers would add scripts to a specific folder and add their entries to the configuration file (kind of like a plugin). 其他开发人员会将脚本添加到特定文件夹,并将其条目添加到配置文件(有点像插件)。 Does anybody have a solution here?
有人在这里有解决方案吗?
Thanks in advance 提前致谢
Thanks for your input... importlib was the way to go. 感谢您的输入... importlib是必经之路。 I used this answer here:
我在这里用这个答案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.