{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 模块和包" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 模块" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python会将所有 `.py` 结尾的文件认定为Python代码文件,考虑下面的脚本 `ex1.py` :" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting ex1.py\n" ] } ], "source": [ "%%writefile ex1.py\n", "\n", "PI = 3.1416\n", "\n", "def sum(lst):\n", " tot = lst[0]\n", " for value in lst[1:]:\n", " tot = tot + value\n", " return tot\n", " \n", "w = [0, 1, 2, 3]\n", "print sum(w), PI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以执行它:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 3.1416\n" ] } ], "source": [ "%run ex1.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "这个脚本可以当作一个模块,可以使用`import`关键词加载并执行它(这里要求`ex1.py`在当前工作目录):" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 3.1416\n" ] } ], "source": [ "import ex1" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ex1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在导入时,**Python**会执行一遍模块中的所有内容。\n", "\n", "`ex1.py` 中所有的变量都被载入了当前环境中,不过要使用\n", "\n", " ex1.变量名\n", "\n", "的方法来查看或者修改这些变量:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.1416\n" ] } ], "source": [ "print ex1.PI" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592653\n" ] } ], "source": [ "ex1.PI = 3.141592653\n", "print ex1.PI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "还可以用\n", "\n", " ex1.函数名\n", "\n", "调用模块里面的函数:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "print ex1.sum([2, 3, 4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "为了提高效率,**Python**只会载入模块一次,已经载入的模块再次载入时,Python并不会真正执行载入操作,哪怕模块的内容已经改变。\n", "\n", "例如,这里重新导入 `ex1` 时,并不会执行 `ex1.py` 中的 `print` 语句:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import ex1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "需要重新导入模块时,可以使用`reload`强制重新载入它,例如:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 3.1416\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reload(ex1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "删除之前生成的文件:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\n", "os.remove('ex1.py')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `__name__` 属性" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "有时候我们想将一个 `.py` 文件既当作脚本,又能当作模块用,这个时候可以使用 `__name__` 这个属性。\n", "\n", "只有当文件被当作脚本执行的时候, `__name__`的值才会是 `'__main__'`,所以我们可以:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ex2.py\n" ] } ], "source": [ "%%writefile ex2.py\n", "\n", "PI = 3.1416\n", "\n", "def sum(lst):\n", " \"\"\" Sum the values in a list\n", " \"\"\"\n", " tot = 0\n", " for value in lst:\n", " tot = tot + value\n", " return tot\n", "\n", "def add(x, y):\n", " \" Add two values.\"\n", " a = x + y\n", " return a\n", "\n", "def test():\n", " w = [0,1,2,3]\n", " assert(sum(w) == 6)\n", " print 'test passed.'\n", " \n", "if __name__ == '__main__':\n", " test()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "运行文件:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "test passed.\n" ] } ], "source": [ "%run ex2.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "当作模块导入, `test()` 不会执行:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import ex2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "但是可以使用其中的变量:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.1416" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ex2.PI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用别名:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3.1416" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import ex2 as e2\n", "e2.PI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 其他导入方法" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以从模块中导入变量:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from ex2 import add, PI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "使用 `from` 后,可以直接使用 `add` , `PI`:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add(2, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "或者使用 `*` 导入所有变量:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "7.5" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from ex2 import *\n", "add(3, 4.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "这种导入方法不是很提倡,因为如果你不确定导入的都有哪些,可能覆盖一些已有的函数。\n", "\n", "删除文件:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\n", "os.remove('ex2.py')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 包" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "假设我们有这样的一个文件夹:\n", "\n", "foo/\n", "- `__init__.py` \n", "- `bar.py` (defines func)\n", "- `baz.py` (defines zap)\n", "\n", "这意味着 foo 是一个包,我们可以这样导入其中的内容:\n", "\n", "```python \n", "from foo.bar import func\n", "from foo.baz import zap\n", "```\n", "\n", "`bar` 和 `baz` 都是 `foo` 文件夹下的 `.py` 文件。\n", "\n", "导入包要求:\n", "- 文件夹 `foo` 在**Python**的搜索路径中\n", "- `__init__.py` 表示 `foo` 是一个包,它可以是个空文件。" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## 常用的标准库" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- re 正则表达式\n", "- copy 复制\n", "- math, cmath 数学\n", "- decimal, fraction\n", "- sqlite3 数据库\n", "- os, os.path 文件系统\n", "- gzip, bz2, zipfile, tarfile 压缩文件\n", "- csv, netrc 各种文件格式\n", "- xml\n", "- htmllib\n", "- ftplib, socket\n", "- cmd 命令行\n", "- pdb \n", "- profile, cProfile, timeit\n", "- collections, heapq, bisect 数据结构\n", "- mmap\n", "- threading, Queue 并行\n", "- multiprocessing\n", "- subprocess\n", "- pickle, cPickle\n", "- struct" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## PYTHONPATH设置" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python的搜索路径可以通过环境变量PYTHONPATH设置,环境变量的设置方法依操作系统的不同而不同,具体方法可以网上搜索。" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }