Mat*_*ell 6 importerror python-3.x
我正在尝试从名为 fractions 的模块中导入名为 gcd 的函数from fractions import gcd。出于某种原因,PyCharm 抛出了一个 ImportError:
from fractions import gcd
ImportError: cannot import name 'gcd' from 'fractions'
Run Code Online (Sandbox Code Playgroud)
我以前有这个工作,我做错了什么?
xjc*_*jcl 22
fractions.gcd(a, b)已移至 Python 3.9 中的 math 模块。所以只需将调用替换为math.gcd(a, b).
事实上,fractions.gcd自 Python 3.5 起就已被弃用(在括号中):
| Python版本 | fractions.gcd(a, b) |
math.gcd(a, b) |
math.gcd(*integers) |
|---|---|---|---|
| Python 3.0 | X | ||
| Python 3.1 | X | ||
| Python 3.2 | X | ||
| Python 3.3 | X | ||
| Python 3.4 | X | ||
| Python 3.5 | (X) | X | |
| Python 3.6 | (X) | X | |
| Python 3.7 | (X) | X | |
| Python 3.8 | (X) | X | |
| Python 3.9 | X | ||
| Python 3.10 | X | ||
| Python 3.11 | X | ||
| Python 3.12 | X |
此外,math.gcd从 Python 3.9 开始,可以采用任意数量的参数(甚至 0 或 1)。
这是旧 networkx 版本的问题。解决这个更新网络x:
conda install -c conda-forge networkx=2.5
Run Code Online (Sandbox Code Playgroud)
你的回溯说 Python 3.9 和文档说 gcd 是数学中的一个函数
在 3.9 版更改: math.gcd() 函数现在用于归一化分子和分母。math.gcd() 总是返回一个 int 类型。以前,GCD 类型取决于分子和分母。
| 归档时间: |
|
| 查看次数: |
5123 次 |
| 最近记录: |