I have this snippet:
Decimal('5,1')
What it should raise (verified on Python 3.6.5):
decimal.InvalidOperation: Invalid literal for Decimal: '5,1'
What I get in a certain Python 3.6.1 environment:
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
Why does this happen?
It seems indeed a bug that was fixed in releases between 3.6.1 and 3.6.5.
My machine has 3.6.1 installed and I could reproduce it:
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal
>>> Decimal('5,1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
>>>
I couldn't spot a specific fix for it in their release notes, but upgrading should do it for you.
Related
I am trying to import socket and I get the following error:
$ python
Python 2.7.16 |Anaconda, Inc.| (default, Mar 14 2019, 16:24:02)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "socket.py", line 68, in <module>
from _ssl import \
ImportError: cannot import name RAND_egd
I thought that perhaps my Anaconda installation was corrupted so I uninstalled and reinstalled Anaconda but the error persists.
I had tried what the other posts suggested except for editing socket.py due to Macos System Integrity Protection. My guess is that the problem lies elsewhere as I doubt that everyone has had to edit the file.
How can I find the source of the problem?
To overcome the limitations of pickle, I switched to multiprocessing_on_dill.
This started to generate an error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 107, in spawn_main
exitcode = _main(fd)
File "C:\ProgramData\Anaconda3\lib\multiprocessing\spawn.py", line 117, in _main
self = reduction.pickle.load(from_parent)
ModuleNotFoundError: No module named '__builtin__'
Is there a way to overcome it?
Surprisingly, it still references C:\ProgramData\Anaconda3\lib\multiprocessing\.
While multiprocessing_on_dill has its own folder C:\ProgramData\Anaconda3\Lib\site-packages\multiprocessing_on_dill with the same set of files.
I'd suggest you use multiprocess instead of multiprocessing_on_dill... the former is better supported, and maintained by the dill author (me). It looks like multiprocess_on_dill is looking for __builtin__, which is where the builtin functions lived in python 2... in python 3, they are in builtins
>$ python
Python 2.7.16 (default, Apr 1 2019, 14:50:56)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import __builtin__
>>>
>$ python
Python 3.6.8 (default, Dec 30 2018, 13:04:41)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import __builtin__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named '__builtin__'
>>> import builtins
>>>
So, I think it's either you are running python 3, and using the python 2 version of the code -- or the module doesn't fully support python 3.
This question already has answers here:
Math domain error in python when using log
(2 answers)
Closed 5 years ago.
when I run this code:
max(MIN_LEARNING_RATE, min(0.5, 1.0 - math.log10((t+1)/25)))
with t = 0
I've this error:
ValueError: math domain error
but if i run the same code with python 3.6 the error disappeared
It's because in Python 2, the division returns a floor of the float, and in the case where it's 1/25, it returns 0.
And math.log(0) gives a domain error.
So in your Python 2 code, add this at the start of the script:
from __future__ import division
By default in Python 2:
$ python2
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.log(1/25)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> 1/25
0
In Python 3:
$ python3
Python 3.6.1 (default, Apr 4 2017, 09:40:21)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.log(1/25)
-3.2188758248682006
>>> 1/25
0.04
In Python 2 with __future__.division:
$ python2
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import division
>>> import math
>>> math.log(1/25)
-3.2188758248682006
>>> 1/25
0.04
Due to rounding in python-2.x, (0+1)/25 evaluates to 1/25, which rounds to 0. Thus, math.log10(0), which is undefined, results in a ValueError.
Either add from __future__ import division to the first line in your program or change the line of code to max(MIN_LEARNING_RATE, min(0.5, 1.0 - math.log10((t+1)/25.0)))
Pypy doesn't seem to handle string.maketrans() when arguments are unicode, however CPython does:
$ python
Python 2.7.5 (default, Oct 11 2013, 14:51:32)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.maketrans(ur"-/[] ", ur"_____")
'\x00\x01\x02\x03\x04\x05\x06...'
$ pypy
Python 2.7.13 (c925e73810367cd960a32592dd7f728f436c125c, Jun 08 2017, 19:14:08)
[PyPy 5.8.0 with GCC 6.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import string
>>>> string.maketrans(ur"-/[] ", ur"_____")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../pypy-5.8-linux_x86_64-portable/lib-python/2.7/string.py", line 78, in maketrans
buf[ord(fromstr[i])] = tostr[i]
TypeError: 'unicode' object cannot be interpreted as an index
Didn't find anything relevant on http://pypy.readthedocs.io/en/latest/cpython_differences.html.
Is this a bug of CPython or PyPy?
That's a "bug", i.e. an unexpected difference. Fixed in 7fe0041fccaa (see line 78 of https://bitbucket.org/pypy/pypy/raw/default/lib-python/2.7/string.py).
Using the code print('{0} is not'.format('That that is not')) in Python 3.1.1, I get the following error:
AttributeError: 'str' object has no attribute 'format'
when I delete the line Netbeans automatically inserted at the beginning:
from distutils.command.bdist_dumb import format
which itself causes an error of
ImportError: cannot import name format
What am I doing wrong here?
You must be running an older version of Python. This does work in Python 3.1.1+:
$ python3
Python 3.1.1+ (r311:74480, Nov 2 2009, 14:49:22)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '{0} is not'.format('That that is not')
'That that is not is not'
You will, however, get this error in Python 2.5.4:
$ python2.5
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '{0} is not'.format('That that is not')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'format'
This feature seems to have been backported to Python 2.6, so you won't get this error there. You must be running Python < 2.6.