我似乎无法使用该__subclasses__()方法列出所有派生类.这是我的目录布局:
import.py
backends
__init__.py
--digger
__init__.py
base.py
test.py
--plugins
plugina_plugin.py
Run Code Online (Sandbox Code Playgroud)
从import.py我打电话来test.py.test.py反过来遍历目录中的所有文件plugins并加载所有文件.test.py看起来像这样:
import os
import sys
import re
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.abspath( __file__ )))))
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(os.path.abspath( __file__ ))), 'plugins'))
from base import BasePlugin
class TestImport:
def __init__(self):
print 'heeeeello'
PLUGIN_DIRECTORY = os.path.join(os.path.abspath(os.path.dirname(os.path.abspath( __file__ ))), 'plugins')
for filename in os.listdir (PLUGIN_DIRECTORY):
# Ignore subfolders
if os.path.isdir (os.path.join(PLUGIN_DIRECTORY, filename)):
continue
else:
if re.match(r".*?_plugin\.py$", filename):
print ('Initialising plugin : ' + filename)
__import__(re.sub(r".py", r"", filename))
print …Run Code Online (Sandbox Code Playgroud)