#!/usr/bin/env python3 import bars print(bars.__name__) bars.hashbar(10) bars.starbar(10) bars.simplebar(10)
from bars import hashbar, simplebar, starbar, andbar, kukacbar hashbar(10) simplebar(5) starbar(5) andbar(8) kukacbar(10) # touch mymodule/__init__.py # help(str) # import os # os.getuid() # os.getpid() # # os.stat(path) # # a = '1' # b = '2' # print int(a) / int(b)
#!/usr/bin/env python3 import bars bars.hashbar(10) bars.starbar(20) from bars import starbar, hashbar hashbar(20) starbar(10)
from bars import simplebar, starbar simplebar(20) starbar(13)
def simplebar(num): """打印 - 分割线 :arg num: 线长 """ print('-' * num) 现在我们启动解释器然后导入我们的模块: >>> import bars >>> 我们必须使用模块名来访问模块内的函数: >>> bars.hashbar(10) ########## >>> bars.simplebar(10) ---------- >>> bars.starbar(10) ********** 导入模块: 有不同的方式导入模块,已经看到了一种,甚至可以从模块中导入指定的函数,如: >>> from bars import simplebar, starbar >>> simplebar(20) -------------------- 当然还可以用from module import * 导入模块中的所有定义,然而这并不是推荐的做法。 (2)包 含有 __init__.py 文件的目录可以用来作为一个包,目录里的所有.py文件都是这个包的子模块。 创建mymodule目录,目录结构如下: tree mymodule mymodule |----bars.py |----__init__.py
import bars # a python script we created to print bars.hashbar(10) bars.simplebar(5) bars.starbar(15) from bars import simplebar, starbar simplebar(11) import os print(os.getuid()) print(os.uname())