近日在vs code做一个python词云的练习时,编译时出现:AttributeError: module 'wordcloud' has no attribute 'WordCloud'的提示,
python3.8环境下,已经安排wordcloud库。
文件代码如下:
#GovRptWordCloudv2.py
import jieba
import wordcloud
from imageio import imread
mask = imread("chinamap.jpg")
excludes = { }
f = open("新时代中国特色社会主义.txt", "r", encoding="utf-8")
t = f.read()
f.close()
ls = jieba.lcut(t)
txt = " ".join(ls)
w = wordcloud.WordCloud(\
width = 1000, height = 700,\
background_color = "white",
font_path = "msyh.ttc", mask = mask
)
w.generate(txt)
w.to_file("grwordcloudm.png")
已安装wordcloud库
运行
import wordcloud
c = wordcloud.WordCloud()
发生异常: AttributeError
partially initialized module 'wordcloud' has no attribute 'WordCloud' (most likely due to a circular import)
File "D:\Python\wordcloud.py", line 12, in w = wordcloud.WordCloud(\File "D:\Python\wordcloud.py", line 3, in import wordcloud
突然发现我的文件名也叫wordcloud,会不会是这个原因?马上将文件改命wordcloud1.py,但还是提示如上的错误,直到最后把wordcloud.py删除,运行正常!
看来在python开发中,要注意源码文件在命名时,要注意不能取和库重名。不然会出现一些看视不应该出现的错误。