LoadingModulesDynamically
This wiki is in the process of being archived due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies. Edits are discouraged.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.
You can load a module dynamically like so:
If you do that, though, "os" isn't bound to anything.
1 >>> __import__("os")
2 <module 'os' from '/usr/lib/python2.3/os.pyc'>
3 >>> os
4 Traceback (most recent call last):
5 File "<stdin>", line 1, in ?
6 NameError: name 'os' is not defined
7 >>>
Once you have the module, you probably want to find classes and functions and data inside of it.
Use getattr: