# Python 模块

## 模块详解

Python 把各种定义存入一个文件，用于在脚本或解释器的交互式实例中，这个文件就是模块。

模块包含可执行语句及函数定义，这些语句用于初始化模块，且仅在 import 语句第一次遇到模块名时执行。

每个模块都有它自己的私有符号表，该表被定义在该模块里的所有函数当作全局符号表使用。

## 模块搜索路径

当导入一个名为 spam 的模块时，解释器首先会搜索具有该名称的内置模块（`sys.builtin_module_names` 中列出了所有内置模块的名称）。如果未找到，它将在变量 `sys.path` 所给出的目录列表中搜索名为 spam.py 的文件。`sys.path` 是从这些位置初始化的:

* 被命令行直接运行的脚本所在的目录（或未指定文件时的当前目录）。
* `PYTHONPATH`（目录列表，与 shell 变量 `PATH` 的语法一样）。
* 依赖于安装的默认值（按照惯例包括一个 site-packages 目录，由 site 模块处理）。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.fantasticmao.cn/tech/python/python-mo-kuai.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
