- The import statement makes all names of a module available, e.g.:
import time
time.sleep(1)
A function that has been imported using this syntax has to be fully
qualified when invoked, modulePath.functionName.
- The import-as statement makes all names of a module available
and assigns a short-hand to the module path:
import sardana.macroserver.macro as ms
This allows you to refer to the Type variable using ms.Type instead
of sardana.macroserver.macro.Type.
- The from-import statement imports names into the
importing modules symbol table. To import single names:
from time import sleep
sleep(1)
To import all module names:
from time import *
Notice that the from-import imported names are referenced
without leading module path, e.g. 'sleep()' NOT 'time.sleep()'.
- Comparing import and from-import we see
that the latter reduces the typing effort. However, considering
bigger software projects the modulePath.functionName notation
enhances the readability of the code.
Therefore the import syntax is recommended,
import-as is of course also ok.
- The function dir() or dir( someObj) returns a list
of names belonging to the respective scope. It is well suited to
understand the effect of import and from-import
statements.
- import and from are executed once. They cannot
be used to refresh modules that have already been loaded.
- The built-in function reload() re-loads a module
which has been imported before. Note that the module
is specified omitting quotes.
import HasyUtils
...
reload( HasyUtils)
- Importing sub-packages is explained in 4.1.
- The dictionary sys.modules contains the loaded modules.
- A module name becomes an attribute after the import.