# 2.1.1 模块

本节目标：

* 了解什么是模块，为什么需要模块
* 尝试引入`turtle`模块
* 学会如何安装模块

## 什么是模块

之前的章节里面我们看到计算机可以直接执行的指令比较有限，我们编程的时候显然不可能每次都从头开始。 一个叫`turtle.py`的Python文件就是一个叫`turtle`的Python模块。 模块中有一些预先写好的代码，让我们可以方便的使用。一个模块通常会定义好一些类、函数、常量等，我们之后会介绍到这些概念。

## 在Python中使用模块

模块需要有自己的名字。在Python中，用

```python
import turtle
```

告诉解释器我们要使用模块`turtle`， 解释器会去寻找叫`turtle.py`的文件。去哪里找呢？ 一般来讲，首先，在“当前目录”找，如果在Spyder中点击“运行”，那么“当前目录”是运行的文件所在的目录。 如果没有找到，会再去Anaconda安装目录下的`Lib`文件夹里面找。如果哪里都找不到，则会报错。

我们需要的`turtle`库便在Anaconda安装目录的`Lib`文件夹里面，如果在当前目录保存了一个`turtle.py`文件， `import turtle`会引入这个`turtle.py`文件。这是需要注意的一点。

此外，其他一些常见的Python库比如`abc`，`six`等，保存文件的时候最好也避免这些名字。 除非已经对`import`的行为非常了解，能够避免出错的情况。

**Learning By Reading 难度：★★ 重要性：★★**

* 关于`import`路径更详细的说明，有兴趣可以看[这里](http://blog.csdn.net/zhouguoqionghai/article/details/69787437)。
* 这些内容比较琐碎，初学可以跳过，以后碰到问题再回头看。

## 安装模块

Anaconda已经预先安装了许多实用的模块，但如果有需要，也可以安装其他模块。

![](/files/-LAYbzglDHTdTriNW5XB)


---

# 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://coding-girls-club.gitbook.io/python-turtle-tutorial/2-python-bian-cheng/2.1-lei-fang-fa-tiao-yong-qian-jin-yu-zhuan-wan/2.1.1-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.
