[oeasy]python070_如何导入模块_导入模块的作用_hello_dunder_双下划线

overmind1980 2025-03-01 10:44:51

如何导入模块_导入模块的作用_hello_dunder_双下划线 回忆上次内容

新函数 dir

可以查询

模块中的成员

添加图片注释,不超过 140 字(可选)

游乐场里面 已经有了一些函数

help

dir

quit

print

这些函数 位于__builtins__模块

所以都是 内建函数

可以把 这个__builtins__删除掉吗?

删除结果

按q

退出帮助

del __builtins__

效果

添加图片注释,不超过 140 字(可选)

内建模块

可以删除

没有报错

瞬间重生

添加图片注释,不超过 140 字(可选)

可以 导入

外部模块 吗?

导入 外部模块

导入命令 是

import

port是港口

import 是进口 、 导入

export 是出口 、 导出

import 后面接空格

被导入的模块 是

__hello__

读作dunder hello

注意 hello 两边

都有dunder(双下划线)

import __hello__

把__hello__模块 导入到游乐场

添加图片注释,不超过 140 字(可选)

可以 输出那句 hello world

变化

注意!

观察导入前后变化

导入__hello__模块后

游乐场中的模块 增加了

添加图片注释,不超过 140 字(可选)

导入 对应关键词

import

怎么 理解 来着?

import

进口(import)

导入模块

可以导入 各种模块 来增强功能

添加图片注释,不超过 140 字(可选)

想要深入了解__hello__

应该怎么办呢?

喊救命

什么不会就 help什么

help(__hello__)

使用help函数 查询手册

添加图片注释,不超过 140 字(可选)

__hello__是

非常简单的

一个模块

里面有个成员变量

initialized

可以看看吗?

观察模块 dir(__hello__)

观察模块

添加图片注释,不超过 140 字(可选)

确实如help所说

有个成员initialized

值为True

为什么要引入 模块 呢?

引入模块的意义

我们可以通过

引入__hello__

完成输出Hello world!

引入traceback

使用跟踪调试

引入time

查看时间

设置延时

添加图片注释,不超过 140 字(可选)

time模块里面有个asctime函数

可以得到当前时间

asctime 中的 asc 什么意思来着?

ascii

asc的意思是ascii

是 7-bit的

字符和序号的固定对应关系

添加图片注释,不超过 140 字(可选)

黑暗森林 上半区

至今还是黑的

可以导入多个文件吗?😱

导入多个文件

根据 下图建议

不同模块

分行导入

添加图片注释,不超过 140 字(可选)

各种 模块

各自身怀绝技

可以配合起来使用

有什么 好玩的 模块 吗?

反重力

在vim的正常模式下

键入:!python3

进入游乐场

在游乐场中键入

import antigravity

添加图片注释,不超过 140 字(可选)

玩完了之后

ctrl + d 退出游乐场

回到vim

观察LICENSE

可以通过vim

看到 python的 许可证

vi /usr/lib/python3.8/LICENSE.txt

这段历史

我们见证过

Guido的个人简历

添加图片注释,不超过 140 字(可选)

我想把 许可证 的 文本

做成 词云图

代码

尝试 加载

分词模块 jieba

词云模块 WordCloud

画图模块 matplotlib

import jieba from wordcloud import WordCloud import matplotlib.pyplot as plt  # 读取文件 with open('/usr/lib/python3.8/LICENSE.txt') as file:     text = file.read()  # 使用jieba进行中文分词 words = ' '.join(jieba.cut(text))  # 生成词云 wordcloud = WordCloud(font_path='/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', # 指定中文字体路径                       width=800, height=600,                       background_color='white').generate(words)  # 使用matplotlib展示词云 plt.imshow(wordcloud, interpolation='bilinear') plt.axis('off') plt.show()

执行结果

ModuleError

找不到模块

新开窗口 :term

在上面开启 shell终端窗口

添加图片注释,不超过 140 字(可选)

安装模块

上面的shell中

粘贴命令

pip install wordcloud pip install matplotlib pip install jieba

在shell中安装类库

添加图片注释,不超过 140 字(可选)

退出shell

在上面的shell中exit退出

添加图片注释,不超过 140 字(可选)

运行

安装类库后

再运行当前程序

成功展示了 词云 图

添加图片注释,不超过 140 字(可选)

可以考虑修改

高度

宽度

背景颜色

这都是细节

问ai就能解决

总结

模块 就是 封装好功能 的 部件

如 wifi模块

导入__hello__module模块

可以 输出hello world!

看起来很简单

添加图片注释,不超过 140 字(可选)

我想要做个自己的模块

能行吗??🤔

我们下次再说!👋

蓝桥->https://www.lanqiao.cn/courses/3584

github->https://github.com/overmind1980/oeasy-python-tutorial

gitee->https://gitee.com/overmind1980/oeasypython

0 阅读:0
overmind1980

overmind1980

此用户有点懒,还没有正式介绍自己