Python报Type Error 'module' is not callable错误如何解决?
问题原因
该TypeError 'module' is not callable报错的核心原因是你当前导入的progressbar是模块对象,并非可调用的函数/类,排除同名文件冲突的情况下基本都是导入方式错误导致的。
解决方案
- 先核对导入语句,如果你当前写的是
import progressbar,不能直接调用progressbar(),要调用模块下的对应方法,正确用法如下:
import progressbar for n in progressbar.progressbar(range(count)):
或者导入时直接引入对应方法:
from progressbar import progressbar for n in progressbar(range(count)):
- 如果你安装的是旧版本progressbar库,对应可调用类名为首字母大写的
ProgressBar,调整用法为:
from progressbar import ProgressBar pbar = ProgressBar() for n in pbar(range(count)):
- 上述方法不生效时先确认库安装正确,执行命令
pip install progressbar2即可,避免安装兼容性差的旧版同名progressbar库。 - 要排查文件冲突可以在代码开头加一行
print(progressbar.__file__),运行后会输出当前导入的progressbar模块对应的文件路径,直接就能确认是不是误导入了自己写的同名py文件,是则重命名本地文件即可解决问题。
内容的提问来源于stack exchange,提问作者Martha R
相关产品推荐
相关产品推荐

