Loading topic
import statement you write gives you a module. This chapter separates two things that are easy to mix up: the file on disk that defines a module, and the module object that exists while your program runs. Later chapters build on this distinc5 min read.py file can be loaded as a module. It covers the import forms, what name each one binds in your namespace, and the errors people hit most often.5 min readimport (import x, from x import y, import x as z). Here you will learn what Python does when it runs an import statement. You will also learn why a module's top-level code 5 min readpython file.py from a terminal, and use a basic import statement. It covers how one file can act as a program and as a library, and how the __main__ guard keeps those roles apart.4 min readimport first checks the sys.modules cache. Here we cover what happens on a cache miss: where Python looks, in what orde6 min readimport and from ... import. It also assumes you know that Python caches imported modules in sys.modules and searches sys.path. Here you will see how a directory becomes a pa5 min readimport and from ... import statements, know that a package is a directory with an __init__.py file, and know that sys.modules caches loaded modules. Here you will see what a dotted name such as shapes.util.mathx means, what Python does when you import one5 min read__init__.py file. It also assumes you know that sys.path lists the places Python searches for top-level modules. Here you will learn how a module insid5 min read__init__.py files, write relative imports, and know that import caches modules in sys.modules. Here you decide which names a package offers to its users and how __all__ takes part in that decision.5 min readimport finds modules through sys.path, how a package is laid out with __init__.py, and how relative imports and the __main__ guard work. It builds on those to explain why python -m exists and when to prefer it over running a file path.5 min readsys.path, packages, relative imports, and __all__. Here you will put those pieces together into a project that you can import from anywhere, test, and share.5 min readsys.modules caches modules and that an import runs the module's code from top to bottom. It also assumes you can read a traceback and have used from x import y and import x. If those are new, read the chapters on import execution and import forms first.5 min readimport binds names, how sys.modules caches modules, and how sys.path is searched. It covers two families of bugs that come from those mechanisms: names that resolve to the wrong object, and code that does too much work when a module loads.6 min readimport forms, the sys.modules cache, and sys.path from earlier chapters. Here you will see the machinery behind import x. You will learn to inspect it, extend it, and use it to debug import problems.4 min read__init__.py, that you know how sys.path is searched, and that you understand how sys.modules caches imports. Here you will learn how a directory with no __init__.py can still be imported, what Python builds in that case, and when the 4 min readimport statement, the sys.modules cache, sys.path, and the idea that a module is an object with its own namespace. Those come from ea4 min read