当前位置: 代码迷 >> 综合 >> Python ModuleNotFoundError: No module named 'ConfigParser'
  详细解决方案

Python ModuleNotFoundError: No module named 'ConfigParser'

热度:75   发布时间:2023-11-25 22:41:29.0

今天在做文件练习时,导入了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^

  相关解决方案