
Meet people who work on similar things as you – get help if you need it
Join Siafoo Now
or
Learn More
Import an arbitrarily named module
0
Updated over 9 years ago (11 Mar 2009 at 03:23 PM)
recent activity
In Brief | Just doing an __import__ only gets you the top level module (importing os.path would only get you os). Use this function to drill down.... more |
Language | Python |
# 's
1def import_module(name):
2 module = __import__(name)
3 if '.' in name:
4 for segment in name.split('.')[1:]:
5 module = getattr(module, segment)
6 return module
Just doing an __import__ only gets you the top level module (importing os.path would only get you os). Use this function to drill down.
Usage:
# 's
1>>> import_module('os.path')
2<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.pyc'>
Add a Comment