python - How to 'import *' and call imported functions when package is directory not file -
if i'm developing client-server app , have 3 files (client.py, server.py, , common.py,) , common.py has useful function (e.g. normalize()), it's easy both client , server this:
from common import * url = normalize(url) however if, various strange reasons, i'd rather have separate subdirectories (client, server, , common), , each function had own file, there doesn't seem similar shortcut.
i have fiddle sys.path, after import need use url=normalize.normalize(url). i'm sure program workaround, there pythonic way of handling i'm unaware of?
update: here's how did after following ignacio's advice below:
$ cat common/__init__.py; client/login.py jcomeauictx.myopenid.com import os, sys module in os.listdir(os.path.dirname(__file__)): print >>sys.stderr, 'module: %s' % module name, extension = os.path.splitext(module) if extension == '.py' , not name.startswith('_'): importer = 'from %s import %s' % (name, name) print >>sys.stderr, 'import statement: %s' % importer exec(importer) result:
module: __init__.py module: normalize.py import statement: normalize import normalize module: __init__.pyc module: normalize.pyc ('http://www.myopenid.com/server', 'http://jcomeauictx.myopenid.com/')
anything __init__.py within directory imports imported on import * provided it's not restricted __all__.
Comments
Post a Comment