导入import
回忆上次内容
- 模块 就是 封装好功能 的 部件
- 导入 __hello__ module模块
新建文件 my_file
vi my file.py
继续观察
退回到shell
下划线的 作用
- 下划线 可以代替 空格
- 而且 能用在 文件名中
- python模块名 需要使用 蛇形命名法
- 编辑 my_file.py
vi my_file.py
- 先试着引入__hello__
- 注意hello两端各有两个下划线
- 然后保存并运行
- 确实在my_file.py中
- 想要再把 time中的asctime
my_file.py
import __hello__
import time
print(time.asctime())
游乐场实战
dir(my_file)
编辑oeasy.py
- 键入:e oeasy.py
- e 指的是 edit 编辑
- 编辑一个oeasy.py
缓存列表
切换 缓存
引入模块
- 打开 oeasy.py
- 在oeasy.py中 只有 一句话
- import my_file
- 导入 my_file模块
运行结果
- 虽然oeasy.py中
- 但是通过 导入my_file模块
自定义模块
import __hello__
import time
print(time.asctime())
print("This is my first module")
切换缓存后
- 切换 缓存
- 来到my_file.py
- 把最后一行
- 修改 为
- pi = 3.14
查看 模块中的变量
import my_file
print(dir(my_file))
尝试 引用模块中的 变量
- 总共两句话
- 1.导入my_file这个模块
- 2.输出my_file模块中 pi这个变量
- 在oeasy.py中
- 导入my_file模块之后
- 尝试输出my_file.pi
尝试直接输出pi
- pi 是模块中的变量
- 想用 就必须带上模块的名字
- my_file.pi
- 中间.的意思是 的
- my_file 模块中的 pi属性
名字空间
- 就像
- 访问 time模块中的
- asctime函数
- time.asctime()
import time
print(time.asctime)
总结
- 这次导入了系统模块
- 自己 定义了 模块
- 导入 my_file.py
- 可以使用
- my_file.pi
- my_file模块中的变量pi
- 但不能直接 用pi
- 如果 就想 直接用pi
- 下次再说
- 蓝桥->https://www.lanqiao.cn/courses/3584
- github->https://github.com/overmind1980/oeasy-python-tutorial
- gitee->https://gitee.com/overmind1980/oeasypython