typeerror:cannot import name 'Iterable' from 'collections'
第一个文件:die.py
from random import randint
‘’’创建掷骰子类’’’
class Die():
def init(self,num_sides=6):
self.num_sides=num_sides
def roll(self):
return randint(1,self.num_sides)
第二个文件:visual.py
from die import Die
‘’’创建实例’’’
die=Die()
results=[]
for roll_num in range(100):
result=die.roll()
results.append(result)
frequencies=[]
for value in range(1,die.num_sides+1):
frequency=results.count(value)
frequencies.append(frequency)
import pygal
hist=pygal.Bar()
hist.title(‘掷骰子100子’)
hist.x_labels=[‘1’,’2’,’3’,’4’,’5’,’6’]
hist.x_title=’结果’
hist.y_title=’结果的频次’
hist.add(‘D6’,frequencies)
hist.render_to_file(‘die_visual.svg’)
typeerror ? 应该是 ImportError ?
这个问题好像在 pygal 2.4.0 已经解决了。妳的 Python是 3.10 的吗 ? 像这样的问题,应该要提供各相关软件的版本,如OS,Python,Library等.
在我的 WIN10, Python 3.95, pygal 2.4.0 运行以下代码没有问题
自 Python 3.10 開始, 庫
collections
以下各項都停用了, 其中就包含了pygal._compat.py
中的from collections import Iterator
pygal
在 pypi 的最新版本為 2.4.0, 但是它的pygal._compat.py
更新並没有加入github.com/Kozea/pygal/blob/master...
更新的內容為
github.com/Kozea/pygal/commit/7796...
因此, 必須自己更改 Lib\site-packages\pygal_compat.py` 的內容
pygal 今天已经在 pypi 中升级到 3.0.0,所以你可以从 pypi 升级它,这个问题将得到解决。
没任何回应,也没打钩?这个问题解决了吗?