Name

The from Statement

Synopsis

from [package.]* module import name [,name]*
from [package.]* module import*
from module import name as othername

Module names access: import (copy) variable names out of a module and use them later without enclosing module name qualification. The from mod import * format copies all names assigned at the top level of the module, except names with a single leading underscore or names not listed in the module’s _ _all_ _ list-of-strings attribute (if defined).

If used, the as clause creates a name synonym. If used, package import paths work the same as in import statements (e.g., from dir1.dir2.mod import X), but the package path needs to be listed only in the from itself. Due to new scoping rules, the “*” format generates warnings in 2.2 if it appears nested in a function or class.

The from statement is also used to enable future (but still experimental) language additions, with from _ _future_ _ import featurename. This format must appear only at the top of a module file (preceded only by an optional doc string).

Get Python Pocket Reference, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.