An `import` statement makes code from another Python module available in the current code. A module can be a `.py` file you wrote or a library module. Import the module before using its functions or other definitions. section 10.2 section 10.6
Choose the form that matches how you want to use the names:
- `import mymath` loads the module; use qualified names such as `mymath.pi` and `mymath.area(2)`. Importing it does not make bare `pi` or `area` available. section 10.2 section 10.3
- `from mymath import pi` makes `pi` directly available, but does not import `area`. You can name several attributes in one statement. section 10.2 section 10.3
- `from mymath import *` makes exported names directly available. Normally this excludes names beginning with `_`; a module’s `__all__` list can instead specify which names it exports. section 10.3
Keep the module name visible
`import cmath` followed by `cmath.sqrt(-1)` produces `1j`. Keeping `cmath.` on the call distinguishes the complex-number function from similarly named functions in `math`. If both modules are imported with `*`, later imports can replace earlier names; wildcard imports also make a name’s origin harder to see. section 4.7.5 section 10.3
Python must be able to find the module: a local file can be imported when it is in the current directory or on the module search path, and third-party modules must be installed somewhere on that path. A missing module raises an error. Editing an already imported file does not bring its changes into the same interactive session; `importlib.reload(mymath)` reloads it. section 3.5 section 10.2 section 10.6
Packages group modules under hierarchical names. Importing a top-level package does not automatically load every submodule: import the one you need, such as `import mathproj.comp.numeric.n1`. Importing it loads its parent packages and executes their `__init__.py` files. Files within a package must also import definitions they use from sibling files. They may use an explicit package path or a relative import such as `from .n2 import h`; relative imports do not work in a module executed as `__main__`. section 18.3.3 section 18.3.4
When a file should work both as an importable module and as a script, put script-only calls under `if __name__ == '__main__':`. Importing the file then leaves those calls unexecuted. section 11.1.3












