How to write a program correctly?
Break the program into several parts, files, directories. projects. Display all dependencies in one place, use the MFCalc principle or its variant. Such code is both easier to read and easier to debug. In most cases, debugging will take you to a few lines of code, not a thousand-line file. Rolling updates of one module, you will not break the rest of the system.
Tests should work successfully, and in different environments (for example, in different versions of Python). Also, tests should be carried out after each change. Instead of doing it manually from the command line, it's easier and faster to create a continuous integration platform.
Automate everything possible. Then document everything in detail. So that whoever receives your code can also follow the plan. Having a plan doesn't just mean looking smarter, it means actually being smarter.