导入模块
********

本章中介绍的模块提供了导入其他Python模块和挂钩以自定义导入过程的新方法
。

本章描述的完整模块列表如下：

* "zipimport" --- 从 Zip 归档导入模块

  * "ZipImportError"

  * zipimporter 对象

    * "zipimporter"

      * "create_module()"

      * "exec_module()"

      * "find_spec()"

      * "get_code()"

      * "get_data()"

      * "get_filename()"

      * "get_source()"

      * "is_package()"

      * "load_module()"

      * "invalidate_caches()"

      * "archive"

      * "prefix"

  * 例子

* "pkgutil" --- 包扩展工具

  * "ModuleInfo"

  * "extend_path()"

  * "get_importer()"

  * "iter_importers()"

  * "iter_modules()"

  * "walk_packages()"

  * "get_data()"

  * "resolve_name()"

* "modulefinder" --- 查找脚本使用的模块

  * "AddPackagePath()"

  * "ReplacePackage()"

  * "ModuleFinder"

    * "report()"

    * "run_script()"

    * "modules"

  * "ModuleFinder" 的示例用法

* "runpy" --- 查找并执行 Python 模块

  * "run_module()"

  * "run_path()"

* "importlib" --- "import" 的实现

  * 概述

  * 函数

    * "__import__()"

    * "import_module()"

    * "invalidate_caches()"

    * "reload()"

  * "importlib.abc" —— 关于导入的抽象基类

    * "MetaPathFinder"

      * "find_spec()"

      * "invalidate_caches()"

    * "PathEntryFinder"

      * "find_spec()"

      * "invalidate_caches()"

    * "Loader"

      * "create_module()"

      * "exec_module()"

      * "load_module()"

    * "ResourceLoader"

      * "get_data()"

    * "InspectLoader"

      * "get_code()"

      * "get_source()"

      * "is_package()"

      * "source_to_code()"

      * "exec_module()"

      * "load_module()"

    * "ExecutionLoader"

      * "get_filename()"

    * "FileLoader"

      * "name"

      * "path"

      * "load_module()"

      * "get_filename()"

      * "get_data()"

    * "SourceLoader"

      * "path_stats()"

      * "path_mtime()"

      * "set_data()"

      * "get_code()"

      * "exec_module()"

      * "load_module()"

      * "get_source()"

      * "is_package()"

    * "ResourceReader"

      * "open_resource()"

      * "resource_path()"

      * "is_resource()"

      * "contents()"

    * "Traversable"

      * "name"

      * "iterdir()"

      * "is_dir()"

      * "is_file()"

      * "joinpath()"

      * "__truediv__()"

      * "open()"

      * "read_bytes()"

      * "read_text()"

    * "TraversableResources"

      * "files()"

  * "importlib.machinery" —— 导入器和路径钩子函数。

    * "SOURCE_SUFFIXES"

    * "DEBUG_BYTECODE_SUFFIXES"

    * "OPTIMIZED_BYTECODE_SUFFIXES"

    * "BYTECODE_SUFFIXES"

    * "EXTENSION_SUFFIXES"

    * "all_suffixes()"

    * "BuiltinImporter"

    * "FrozenImporter"

    * "WindowsRegistryFinder"

    * "PathFinder"

      * "find_spec()"

      * "invalidate_caches()"

    * "FileFinder"

      * "path"

      * "find_spec()"

      * "invalidate_caches()"

      * "path_hook()"

    * "SourceFileLoader"

      * "name"

      * "path"

      * "is_package()"

      * "path_stats()"

      * "set_data()"

      * "load_module()"

    * "SourcelessFileLoader"

      * "name"

      * "path"

      * "is_package()"

      * "get_code()"

      * "get_source()"

      * "load_module()"

    * "ExtensionFileLoader"

      * "name"

      * "path"

      * "create_module()"

      * "exec_module()"

      * "is_package()"

      * "get_code()"

      * "get_source()"

      * "get_filename()"

    * "NamespaceLoader"

    * "ModuleSpec"

      * "name"

      * "loader"

      * "origin"

      * "submodule_search_locations"

      * "loader_state"

      * "cached"

      * "parent"

      * "has_location"

    * "AppleFrameworkLoader"

      * "name"

      * "path"

  * "importlib.util" —— 导入器的工具程序代码

    * "MAGIC_NUMBER"

    * "cache_from_source()"

    * "source_from_cache()"

    * "decode_source()"

    * "resolve_name()"

    * "find_spec()"

    * "module_from_spec()"

    * "spec_from_loader()"

    * "spec_from_file_location()"

    * "source_hash()"

    * "_incompatible_extension_module_restrictions()"

    * "LazyLoader"

      * "factory()"

  * 例子

    * 用编程方式导入

    * 检查某模块可否导入。

    * 直接导入源码文件。

    * 实现延迟导入

    * 导入器的配置

    * "importlib.import_module()" 的近似实现

* "importlib.resources" -- 包资源的读取、打开和访问

  * "Anchor"

  * "files()"

  * "as_file()"

  * 函数式 API

    * "open_binary()"

    * "open_text()"

    * "read_binary()"

    * "read_text()"

    * "path()"

    * "is_resource()"

    * "contents()"

* "importlib.resources.abc" -- 资源的抽象基类

  * "ResourceReader"

    * "open_resource()"

    * "resource_path()"

    * "is_resource()"

    * "contents()"

  * "Traversable"

    * "name"

    * "iterdir()"

    * "is_dir()"

    * "is_file()"

    * "joinpath()"

    * "__truediv__()"

    * "open()"

    * "read_bytes()"

    * "read_text()"

  * "TraversableResources"

    * "files()"

* "importlib.metadata" -- 访问软件包元数据

  * 概述

    * "PackageNotFoundError"

  * 函数式 API

    * 入口点

      * "entry_points()"

      * "EntryPoints"

      * "EntryPoint"

    * 分发的元数据

      * "metadata()"

      * "PackageMetadata"

    * 分发包的版本

      * "version()"

    * 分发包的文件

      * "files()"

      * "PackagePath"

    * 分发包的依赖

      * "requires()"

    * 将导入映射到分发包

      * "packages_distributions()"

  * 分发包对象

    * "distribution()"

    * "Distribution"

  * 分发包的发现

  * Implementing Custom Providers

    * 示例

* "sys.path" 模块搜索路径的初始化

  * Virtual Environments

  * _pth 文件

  * 嵌入式 Python
