AttributeError: module 'fractions' has no attribute 'Fraction'
AttributeError: module 'fractions' has no attribute 'Fraction'
我想在 Python 开始学习分数模块。我使用 Atom 编辑器并为编辑器中的 运行 模块打包 "Script"。所以,我在尝试制作简单的分数对象时遇到了这个错误:
AttributeError: module 'fractions' has no attribute 'Fraction'
代码:
import fractions
f = fractions.Fraction(3, 4) + 1 + 1.5
print(f)
怎么了? Python intrepreter 使用它没有任何问题。
您有一个屏蔽库的本地 fractions.py
脚本。 Python 将在标准库之前在当前目录中查找导入,因此请确保您给脚本的名称与要使用的库的名称不同!
您可以通过打印导入的模块来找出该文件所在的位置:
import fractions
print(fractions)
重命名或删除该文件。
我想在 Python 开始学习分数模块。我使用 Atom 编辑器并为编辑器中的 运行 模块打包 "Script"。所以,我在尝试制作简单的分数对象时遇到了这个错误:
AttributeError: module 'fractions' has no attribute 'Fraction'
代码:
import fractions
f = fractions.Fraction(3, 4) + 1 + 1.5
print(f)
怎么了? Python intrepreter 使用它没有任何问题。
您有一个屏蔽库的本地 fractions.py
脚本。 Python 将在标准库之前在当前目录中查找导入,因此请确保您给脚本的名称与要使用的库的名称不同!
您可以通过打印导入的模块来找出该文件所在的位置:
import fractions
print(fractions)
重命名或删除该文件。