对 Python 的模块不能进行`.`导入吗?

2018-08-13 11:02:35 +08:00
 codechaser

举例:想要引入 matplotlib 下面的 animation 模块,import matplotlib.animation会出错,from matplotlib import animation不会。

2215 次点击
所在节点    Python
9 条回复
imn1
2018-08-13 11:07:10 +08:00
看标题还以为你 powershell 写多了

import matplotlib
matplotlib.animation ...

import matplotlib as a
a.animation ...
codechaser
2018-08-13 11:09:29 +08:00
@imn1 我刚才试了一下,`import matplotlib.animation as animation`也可以。。。
DiffView
2018-08-13 11:20:42 +08:00
我还以为是 from .xxx import bbb 呢
mrchi
2018-08-13 11:56:04 +08:00
![屏幕快照.png]( https://i.loli.net/2018/08/13/5b71012aad08e.png)

没有报错 Ubuntu 16.04
codechaser
2018-08-13 15:08:45 +08:00
@mrchi 头像好评😂
datou
2018-08-13 17:12:31 +08:00
python3.6.5 可以使用楼主的引用模块方式

没问题
gjquoiai
2018-08-14 18:15:24 +08:00
emm 我猜报错肯定不是 import 报错吧?
如果你写 import matplotlib.animation,调用的时候就得写 matplotlib.animation()
如果你写 from matplotlib import animation,调用的时候就可以写 animation()
codechaser
2018-08-14 20:28:19 +08:00
@gjquoiai 是这样的,老哥。我看了一下 tutorial 里的关于包的那一章,感觉有点收获🤔
ps1aniuge
2018-08-14 22:40:39 +08:00
powershell 就是好。
import matplotlib
matplotlib.animation
反人类。
==================
在 ps 中,第一句没必要。
在 ps 中,第二句也可以省略点前面的部分。
因为 ps 的方法,命令,函数都是不重名的。导入的时候,必须 显式! 允许重名。
当然了,你这么做之后,点前面的内容 就需要了。
==================
这就好像,领导直接含张三。就是叫自己部门张三。
如果有两个张三,就喊 销售部.张三。一个道理。
==================
我说的大家能听懂么? ps 这里就是好。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/479244

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX