# python的模块和包

模块是用来组织Python代码的方法，包则是用来组织模块的。

模块从逻辑上组织Python代码变短，这些代码段相互间有一定联系，可能是一个包含数据成员和方法的类，也可能是一组相关但彼此独立的操作函数。这些代码段是共享的，所以Python允许`import`一个模块，以实现代码重用。

## 模块

### 模块名称空间

模块的导入需要一个叫做`路径搜索`的过程，即在文件系统的`预定义区域`查找模块文件，这些预定义区域是Python搜索路径的集合。

默认搜索路径是在编译或者安装时指定的，可以通过 `sys.path` 来获得当前搜索路径

如果要增加模块的搜索露尽搞，可以使用 `append()` 方法

```
sys.path.append('/home/wesc/py/lib')
```

然后就可以使用

```
import mymodule
```

添加模块

> `sys.modules` 可以找到当前导入了哪些模块以及模块来自哪里。和`sys.path`不同，`sys.modules`是一个字典，使用模块名作为key，对应的物理地址作为value。

## 参考

* 《Python核心编程》


---

# 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://huataihuang.gitbook.io/cloud-atlas-draft/develop/python/startup/module_and_package.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.
