今天在做文件练习时,导入了ConfigParser模块,执行代码报错:ModuleNotFoundError: No module named 'ConfigParser'
代码如下:
# coding=utf-8
# 文件练习
import ConfigParser
# 创建一个对象
cfg = ConfigParser.ConfigParser()
cfg.read('imoocFileTest.txt')
print(cfg.sections())
报错:
Traceback (most recent call last):
File "D:/.../fileFinallyTest.py", line 4, in <module>
import ConfigParser
ModuleNotFoundError: No module named 'ConfigParser'
原因:
在 Python 3.x 版本后,ConfigParser.py 已经更名为 configparser.py 所以出错!
更改:
import configparser
# 创建一个对象 cfg = configparser.ConfigParser() cfg.read('imoocFileTest.txt') print(cfg.sections())
将导入的模块名改为小写的configparser即可。
喜欢的支持一下哦^v^