dispatcher python
<p>hy all, I have the following "wrong" dispatcher:</p>

<pre><code>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)
</code></pre>

<p>It produces the following error:</p>

<p>ImportError: No module named commands.CommandAntitheft</p>

<p>where Command*.py is placed into modules\commands\ folder</p>

<p>can someone help me?</p>

<p>One Possible solution (It works!!!) is:</p>

<pre><code>    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)
</code></pre>

<p>It still remains all risks suggested by bobince (tanks :-) )but now I'm able to load commands at "runtime"</p>
