Skip to content
Advertisement

Import Error: can’t import name gcd from fractions

I’m trying to import a function called gcd from a module called fractions with from fractions import gcd. For some reason, PyCharm throws an ImportError:

    from fractions import gcd
ImportError: cannot import name 'gcd' from 'fractions' 

I had this working before, what am I doing wrong?

Advertisement

Answer

Your traceback says Python 3.9 and the documentation says gcd is a function in math

Changed in version 3.9: The math.gcd() function is now used to normalize the numerator and denominator. math.gcd() always return a int type. Previously, the GCD type depended on numerator and denominator.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement