Starting with Python 3.3, the hashing algorithm is non-deterministically salted to avoid a certain kind of attack. This is nice for webservers but it's a pain when trying to debug a program: Every time I run my script, dict contents are iterated in a different order.
Some earlier versions of python had a -R
flag for enabling hash randomization, but now that it's the default behavior, the flag has not been replaced by its opposite.
Randomization can be disabled by setting the environment variable PYTHONHASHSEED
:
PYTHONHASHSEED
If this variable is not set or set to random, a random value is used to seed the hashes of str, bytes and datetime objects.
If PYTHONHASHSEED is set to an integer value, it is used as a fixed seed for generating the hash() of the types covered by the hash randomization.
The catch is that this variable must be set before launching the python process. I've tried to set it with os.putenv()
, or in os.environ
, but these seem to have no effect on the hashing method. This is not too surprising: I wouldn't expect python to check the environment before every single set or dictionary lookup! So, the question remains:
Is there a way for a python program to disable its own hash randomization?
See Question&Answers more detail:os