On Fri, 21 Oct 2011 07:05:32 -0700, Shane wrote: > Need to refine a question I asked earlier. If I have a module, > > |-- foo > |-------| > |-------|---bar > |-------|-------| > |-------|-------|---__init__.py > > then I can say import foo.bar
No you can't, not the way you have listed it. As shown, foo is just a directory, so "import foo" will fail. However, "import bar" may work, provided foo/bar is in the module search path. Perhaps you mean you have a package, with a sub-package: foo/ +-- __init__.py +-- bar/ ... +-- __init__.py Now you have TWO modules, foo and foo.bar, and you can "import foo.bar" successfully. > But suppose I want to import foo.bar.stuff and stuff isn't located on > under `bar' because it's user > supplied code: > > |- stuff <- I want this to be logically > under foo.bar > |-------|---__init__.py <- even though it's not under bar in > the computer's file system This is not clear what you are trying to do. Please explain more clearly what your module layout is. foo/ +-- __init__.py +-- stuff.py +-- bar/ ... +-- __init__.py Or: stuff.py foo/ +-- __init__.py +-- bar/ ... +-- __init__.py But since stuff is supposed to be a plugin, the most obvious, sensible way to lay out the modules would be: foo/ +-- __init__.py +-- plugins/ ... +-- __init__.py ... +-- stuff.py +-- bar/ ... +-- __init__.py and then "import foo.plugins.stuff". Clear, simple and obvious. > Now what: how to arrange to do command: import foo.bar.stuff Why do you want to call it foo.bar.stuff when it isn't actually foo.bar.stuff? Anyone trying to debug this will hate you, when they try to find a module foo/bar/stuff.py and can't find it because it doesn't exist. If you must play games with module locations, put this inside the foo/bar/__init__.py module: import foo.plugins.stuff as stuff and now you can say "import foo.bar.stuff". But why bother? Of course, plugin discovery is still a problem, but that's still a problem no matter what you do. Best to use a well-tested plugin library instead of trying to invent your own. -- Steven -- http://mail.python.org/mailman/listinfo/python-list