Python writes bytecode on import by default when such files do not already exist, which is achieveable when the executing user has write privileges in the directories of the modules being imported. This is problematic when running as root, both in port build and system administration/operation scenarios. When building ports as root, including the optional interactive shell phase, written bytecode results in filesystem violations. In system administration/operation, when system services are written in Python and executed by root, bytecode gets written to system directories, again resulting in filesystem pollution and integrity violations.
The Python interpreter has both a -B flag and PYTHONDONTWRITEBYTECODE environment variable to disable the default behaviour. However, this assumes that the flag or environment variable can be passed/set at will, which is not the case in either scenario described above. There also exists a sys.dont_write_bytecode settable variable in the Python standard library, but this is only useful inside a Python script or interactive session.
This unconditionally disables writing bytecode when the effective UID is 0, which corresponds to root. There are three variants of this patch: 3.7, 3.8-3.10, 3.11 and later.