Online Book Reader

Home Category

Learning Python - Mark Lutz [327]

By Root 1613 0
in Chapter 11—the end keyword is 3.0-only):

C:\Users\veramark\Mark> c:\Python30\python mydir.py

------------------------------------------------------------

name: mydir file: C:\Users\veramark\Mark\mydir.py

------------------------------------------------------------

00) seplen 60

01) __builtins__

02) __file__

03) __package__

04) listing

05) __name__

06) sepchr -

07) __doc__

------------------------------------------------------------

mydir has 8 names

------------------------------------------------------------

To use this as a tool for listing other modules, simply pass the modules in as objects to this file’s function. Here it is listing attributes in the tkinter GUI module in the standard library (a.k.a. Tkinter in Python 2.6):

>>> import mydir

>>> import tkinter

>>> mydir.listing(tkinter)

------------------------------------------------------------

name: tkinter file: c:\PYTHON30\lib\tkinter\__init__.py

------------------------------------------------------------

00) getdouble

01) MULTIPLE multiple

02) mainloop

03) Canvas

04) AtSelLast

...many more name omitted...

151) StringVar

152) ARC arc

153) At

154) NSEW nsew

155) SCROLL scroll

------------------------------------------------------------

tkinter has 156 names

------------------------------------------------------------

We’ll meet getattr and its relatives again later. The point to notice here is that mydir is a program that lets you browse other programs. Because Python exposes its internals, you can process objects generically.[55]

* * *

[54] As we saw in Chapter 17, because a function can access its enclosing module by going through the sys.modules table like this, it’s possible to emulate the effect of the global statement. For instance, the effect of global X; X=0 can be simulated (albeit with much more typing!) by saying this inside a function: import sys; glob=sys.modules[__name__]; glob.X=0. Remember, each module gets a __name__ attribute for free; it’s visible as a global name inside the functions within the module. This trick provides another way to change both local and global variables of the same name inside a function.

[55] Tools such as mydir.listing can be preloaded into the interactive namespace by importing them in the file referenced by the PYTHONSTARTUP environment variable. Because code in the startup file runs in the interactive namespace (module __main__), importing common tools in the startup file can save you some typing. See Appendix A for more details.

Importing Modules by Name String

The module name in an import or from statement is a hardcoded variable name. Sometimes, though, your program will get the name of a module to be imported as a string at runtime (e.g., if a user selects a module name from within a GUI). Unfortunately, you can’t use import statements directly to load a module given its name as a string—Python expects a variable name, not a string. For instance:

>>> import "string"

File "", line 1

import "string"

^

SyntaxError: invalid syntax

It also won’t work to simply assign the string to a variable name:

x = "string"

import x

Here, Python will try to import a file x.py, not the string module—the name in an import statement both becomes a variable assigned to the loaded module and identifies the external file literally.

To get around this, you need to use special tools to load a module dynamically from a string that is generated at runtime. The most general approach is to construct an import statement as a string of Python code and pass it to the exec built-in function to run (exec is a statement in Python 2.6, but it can be used exactly as shown here—the parentheses are simply ignored):

>>> modname = "string"

>>> exec("import " + modname) # Run a string of code

>>>

Return Main Page Previous Page Next Page

®Online Book Reader