2

I am trying to do some simple decimal math to practice with the Tkinter GUI, but for some reason I cannot import Decimal:

>>> from decimal import Decimal
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 139, in <module>
    import math as _math
  File "math.py", line 3, in <module>
    from decimal import Decimal
ImportError: cannot import name Decimal

I am using Python 2.7.11 This is making me feel pretty stupid since it seems like a simple thing to do. Is Decimal not supported or am I doing this wrong?

Michael W
  • 39
  • 1
  • 3

2 Answers2

4

You called a file math.py, meaning it overrides the built-in math module and breaks everything that uses that module. Pick a different name, and the problem will go away.

user2357112
  • 260,549
  • 28
  • 431
  • 505
2

I was having the same issue of Python 3+ not being able to import Decimal. Even some external libraries, like pygal, would not install because the system could not access Decimal and decimal library.

I found accidentally in another site that "there maybe another decimal.py file in your system, shadowing on the decimal library".

I checked and found out that I had named a test file (while I was learning Python) as decimal.py and after I renamed this to decimaltest.py everything is working perfect.

Check if you have done the same or you have any similar wrongly named file in your system.

Cadoiz
  • 1,446
  • 21
  • 31
Georgios
  • 21
  • 1