This section elaborates a bit on importing modules. Suppose we have the following package structure:
|-- main.py `-- p1 |-- __init__.py # empty |-- mod1.py # contains class cl1 `-- p2 |-- __init__.py # empty `-- mod2.py # contains class cl2
The program main.py
refers to mod1
and mod2
belonging
to the packages p1
and p2
with p2
being the
sub-package of p1
.
The from-import syntax brings the symbols of mod1 and mod2 directly into mains name space. Therefore main can use them without further qualification:
#!/usr/bin/env python from p1.mod1 import * from p1.p2.mod2 import * o = cl1() o = cl2() # # the following print-statement suppresses the default symbols like '__builtins__', '__doc__' and so # print( "global symbol table:", [sym for sym in dir() if sym.find("__") == -1]) # print-out: # this is p1.mod1.cl1.__init__ # this is p1.p2.mod2.cl2.__init__ # global symbol table: ['cl1', 'cl2', 'o'] #
The import statement, see below, brings only p1 into mains name space:
#!/usr/bin/env python import p1.mod1 import p1.p2.mod2 o = p1.mod1.cl1() o = p1.p2.mod2.cl2() print( "global symbol table:", [sym for sym in dir() if sym.find("__") == -1]) # print-out: # this is p1.mod1.cl1.__init__ # this is p1.p2.mod2.cl2.__init__ # global symbol table: ['o', 'p1'] #
You may ask yourself whether the line import p1.p2.mod2 of the example above is really needed. In fact it is not. The module p2.mod2 can be made available through p1 by adding import p2.mod2 to the p1 package init file, p1/__init__.py.
Packages may refer to sub-modules. The from-.-import syntax is a relative path specification:
|-- main.py `-- p1 |-- __init__.py # contains 'from . import mod1Defs' |-- mod1.py |-- mod1Defs.py # contains 'gConst = 9.81' `-- p2 |-- __init__.py `-- mod2.py
This is how main.py makes use of a definition from p1/mod1Defs.py
:
#!/usr/bin/env python import p1.mod1 import p1.p2.mod2 o = p1.mod1.cl1() o = p1.p2.mod2.cl2() print( "gConst", p1.mod1Defs.gConst) print( "main global symbol table:", [sym for sym in dir() if sym.find("__") == -1]) print( "p1 global symbol table:", [sym for sym in dir(p1) if sym.find("__") == -1]) # print-out: # this is p1.mod1.cl1.__init__ # this is p1.p2.mod2.cl2.__init__ # gConst 9.81 # main global symbol table: ['o', 'p1'] # p1 global symbol table: ['mod1', 'mod1Defs', 'p2']