import matplotlib
fig = matplotlib.pyplot.figure()
我使用这种导包的方法是不能用的,会报**AttributeError: 'module' object has no attribute 'pyplot'**错误。
import matplotlib.pyplot as plt
fig = plt.figure()
这种是正常的导包。
pyplot是matplotlib的子模块,所以Python是不可以只导入母包么?
但是为什么numpy只需要import numpy就可以用了呢?
子包并不会自动导入;
可以查看matplotlib的__init__.py文件,与numpy的__init__.py文件;
发现numpy在init文件中import了所有子包,所以可以通过numpy.sub的形式调用子包,其实是numpy init 中已经导入了;
而matploblib并没有 所以不可以;