3

I have a directory structure like so:

|- project
 |- commands.py
 |- Modules
 | |- __init__.py
 | |- base.py
 | \- build.py
 \- etc....

I have the following code in __init__.py

commands = []
hooks = []
def load_modules():
 """ dynamically loads commands from the /modules subdirectory """
 path = "\\".join(os.path.abspath(__file__).split("\\")[:-1])
 modules = [f for f in os.listdir(path) if f.endswith(".py") and f != "__init__.py"]
 print modules
 for file in modules:
 try:
 module = __import__(file.split(".")[0])
 print module
 for obj_name in dir(module):
 try:
 potential_class = getattr(module, obj_name)
 if isinstance(potential_class, Command):
 #init command instance and place in list
 commands.append(potential_class(serverprops))
 if isinstance(potential_class, Hook):
 hooks.append(potential_class(serverprops))
 except:
 pass
 except ImportError as e:
 print "!! Could not load %s: %s" % (file, e)
 print commands
 print hooks

I'm trying to get __init__.py to load the appropriate commands and hooks into the lists given, however i always hit an ImportError at module = __import__(file.split(".")[0]) even though __init__.py and base.py etc are all in the same folder. i have verified that nothing in any of the module files requires anything in __init__.py, so i'm really at a loss of what to do.

asked Dec 21, 2010 at 4:00
4
  • __import__ has quite a few fiddly bits you need to be aware of, especially to do with fromlist. Try searching for that and see if you can figure it out. Commented Dec 21, 2010 at 4:05
  • path = os.path.dirname(os.path.abspath(__file__)) (more correct and incidentally cross-platform) Commented Dec 21, 2010 at 4:09
  • From where and how is load_modules() invoked? Commented Dec 21, 2010 at 8:40
  • load_modules is invoked from commands.py during init Commented Dec 21, 2010 at 13:21

1 Answer 1

2

All you're missing is having Modules on the system path. Add

import sys
sys.path.append(path)

after your path = ... line and you should be set. Here's my test script:

import os, os.path, sys
print '\n'.join(sys.path) + '\n' * 3
commands = []
hooks = []
def load_modules():
 """ dynamically loads commands from the /modules subdirectory """
 path = os.path.dirname(os.path.abspath(__file__))
 print "In path:", path in sys.path
 sys.path.append(path)
 modules = [f for f in os.listdir(path) if f.endswith(".py") and f != "__init__.py"]
 print modules
 for file in modules:
 try:
 modname = file.split(".")[0]
 module = __import__(modname)
 for obj_name in dir(module):
 print '%s.%s' % (modname, obj_name )
 except ImportError as e:
 print "!! Could not load %s: %s" % (file, e)
 print commands
load_modules()
answered Jan 16, 2011 at 16:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.