Please do not use these obsolete Python libraries anymore!

Beck Moulton
3 min readSep 2, 2024

With the release of each Python version, new modules are added and new and better ways of doing things are introduced. Although we are used to using good old Python libraries and certain ways of doing things, it is now time to upgrade and utilize new and improved modules and their features.

Pathlib instead of OS

When it comes to the OS module, another part that you should stop using is os.urandom. Instead, you should use the new secret modules available since Python 3.6:

#The old way: import os length = 64 value = os.urandom(length) print(f"Bytes: {value}") # Bytes: b'\xfa\xf3...\xf2\x1b\xf5\xb6' print(f"Hex: {value.hex()}") # Hex: faf3cc656370e31a938e7...33d9b023c3c24f1bf5 #New approach: import secrets value = secrets.token_bytes(length) print(f"Bytes: {value}") # Bytes: b'U\xe9n\x87...\x85>\x04j:\xb0' value = secrets.token_hex(length) print(f"Hex: {value}") # Hex: fb5dd85e7d73f7a08b8e3...4fd9f95beb08d77391

The use of os.urandom is not actually the problem here. The reason for introducing the secrets module is that people use random modules to generate passwords, even if the random module does not generate password security tokens.

According to the documentation, the random module should not be used for security purposes, and you should use secrets or os.urandom. However, the secrets module is definitely more preferable as it is relatively new and includes some utilities/convenience methods for hexadecimal…

--

--

Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton