したがって、次のようなディレクトリツリーがあります。
pluginlist.py
plugins/
__init__.py
plugin1.py
plugin2.py
...
そして、plugin1、plugin2 などのそれぞれから同様の名前の辞書を連結したいとします。
私がこれを行っている方法は次のとおりです(pluginlist.pyから):
import os
pluginFolderName = "plugins"
pluginFlag = "##&plugin&##"
commands = {}
os.chdir(os.path.abspath(pluginFolderName))
for file in os.listdir(os.getcwd()):
if os.path.isfile(file) and os.path.splitext(file)[1] == ".py":
fileo = open(file, 'r')
firstline = fileo.readline()
if firstline == "##&plugin&##\n":
plugin_mod = __import__("plugins.%s" % os.path.splitext(file)[0])
import_command = "plugin_commands = plugin_mod.%s" % os.path.splitext(file)[0]
exec import_command
commands = dict(commands.items() + plugin_commands.commands.items())
print commands
(印刷コマンドはテスト用です)
これを Windows で実行すると適切なコマンド ディクショナリが得られますが、Linux (Ubuntu Server) で実行すると空のディクショナリが得られます。