Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to import decimal in Python 2.7 or Python 3.3 [duplicate]

I am unable to import decimal in the terminal for Python 2.7 or 3.3.

Here are the errors I get:

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 01:25:11) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import decimal
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/decimal.py", line 3849, in <module>
    _numbers.Number.register(Decimal)
AttributeError: 'module' object has no attribute 'Number'

or Python 2.7

Python 2.7.2 (default, Oct 11 2012, 20:14:37) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import decimal
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 141, in <module>
    import numbers as _numbers
  File "numbers.py", line 34, in <module>
    assert x / y == 2.5 # true division of x by y
AssertionError

How to I import decimal?

like image 401
ubersquared Avatar asked Aug 04 '13 16:08

ubersquared


People also ask

What is decimal () in Python?

In Python, there is a module called Decimal, which is used to do some decimal floating point related tasks. This module provides correctly-rounded floating point arithmetic. To use it at first we need to import it the Decimal standard library module. import decimal.

Should I use float or decimal Python?

For most use cases, I recommend using decimals. If you initialize them with strings, you prevent subtle bugs and get the increased precision benefits. If you need to eliminate all subtle rounding issues, use the fractions module. Even though floats perform better than decimals, I recommend avoiding floats.


2 Answers

Is there numbers.py in current working directory?

That could be the reason of the problem, because that prevent import of standard library module numbers.

like image 183
falsetru Avatar answered Oct 04 '22 22:10

falsetru


How to import decimal in Python3:

from decimal import Decimal
a = Decimal(25)
print(type(a))

//prints <class 'decimal.Decimal'>
like image 27
Eric Leschinski Avatar answered Oct 04 '22 20:10

Eric Leschinski