GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Wiki
  • PSF Wiki
  • Jython Wiki
  • Python Wiki
  • Archive

This view

  • Original page
  • New address

GeezerVison is showing wiki.python.org — Loading Modules Dynamically

View as Markdown

Loading Modules Dynamically¶

Legacy Wiki Page

This page was migrated from the old MoinMoin-based wiki. Information may be outdated or no longer applicable. For current documentation, see python.org.

You can load a module dynamically like so:

   1 __import__("os")

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:

   1 >>> m=__import__("mine")
   2 >>> m
   3 <module 'mine' from 'mine.pyc'>
   4 >>> getattr(m, "eggs")
   5 4654
   6 >>> getattr(m, "ham")
   7 'Monty Python'
Previous
Lo Gix
Next
Local Names Description
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.