Python中使用Enum类时出现cannot import name ‘Enum‘ from partially initialized module ‘enum‘
程序员文章站
2022-03-11 21:19:17
Python中使用Enum类时出现cannot import name ‘Enum’ from partially initialized module ‘enum’错误信息Traceback (most recent call last): File "D:/software/PyCharm/python2020/pythonCode/FirstProject/lxf/classPlus/enum.py", line 3, in from enum impor...
Python中使用Enum类时出现cannot import name ‘Enum’ from partially initialized module ‘enum’
错误信息
Traceback (most recent call last):
File "D:/software/PyCharm/python2020/pythonCode/FirstProject/lxf/classPlus/enum.py", line 3, in <module>
from enum import Enum
File "D:\software\PyCharm\python2020\pythonCode\FirstProject\lxf\classPlus\enum.py", line 3, in <module>
from enum import Enum
ImportError: cannot import name 'Enum' from partially initialized module 'enum' (most likely due to a circular import) (D:\software\PyCharm\python2020\pythonCode\FirstProject\lxf\classPlus\enum.py)
这是我的代码(代码非原创,这里是测试)
from enum import Enum
Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
for name, member in Month.__members__.items():
print(name, '=>', member, ',', member.value)
可以看出代码没有问题
问题出在文件名上
出现问题的原因:python会优先引用本地你创建的文件名
from enum import Enum
这个导入的是你这个文件,而不是python的枚举类
解决方法:将这个文件的名字改成enum_test
本文地址:https://blog.csdn.net/zphuangtang/article/details/107657925