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