简体   繁体   中英

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 .

In the JSON-file there are multiple entries containing different filenames and the name of a method within each file.

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.

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. I used this answer here:

How to import a module given the full path?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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