HY一切,我有以下的 「錯誤」 的調度員:調度蟒蛇
def _load_methods(self):
import os, sys, glob
sys.path.insert(0, 'modules\commands')
for c in glob.glob('modules\commands\Command*.py'):
if os.path.isdir(c):
continue
c = os.path.splitext(c)[0]
parts = c.split(os.path.sep)
module, name = '.'.join(parts), parts[-1:]
module = __import__(module, globals(), locals(), name)
_cmdClass = __import__(module).Command
for method_name in list_public_methods(_cmdClass):
self._methods[method_name] = getattr(_cmdClass(), method_name)
sys.path.pop(0)
它產生以下錯誤:
導入錯誤:沒有模塊名爲commands.CommandAntitheft
where命令*。 py被放入modules \ commands \文件夾中
有人可以幫助我嗎?
一個可能的解決方案(它的工作原理!!!)是:
def _load_methods(self):
import os, sys, glob, imp
for file in glob.glob('modules/commands/Command*.py'):
if os.path.isdir(file):
continue
module = os.path.splitext(file)[0].rsplit(os.sep, 1)[1]
fd, filename, desc = imp.find_module(module,
['./modules/commands'])
try:
_cmdClass = imp.load_module(module, fd, filename, desc).Command
finally:
fd.close()
for method_name in list_public_methods(_cmdClass):
self._methods[method_name] = getattr(_cmdClass(), method_name)
它仍然通過bobince建議(坦克:-))的所有風險,但現在我能夠在「運行」加載命令
嗨@ DrFalk3n我偶然發現了你的舊問題。時代已經發生了變化,這個問題並沒有真正符合當前有關什麼樣的問題的指導方針。你想讓它保持這種方式還是自己編輯它,或者我可以繼續編輯它? – Breeze 2016-07-07 11:10:50
請隨時改變它,thkk – DrFalk3n 2016-07-08 15:00:02