You probably noticed that almost all of the Python code imports at the top of the file. The reason is that the import overhead is minimal, and the likelihood that you will be importing code at some point in the process life cycle, so you can also get rid of it.
The only good reason to import at the function level is to avoid circular dependencies.
Change Your comments indicate that you did not understand how web applications typically work, at least in Python. They do not start a new process for each request and do not import code from scratch. Rather, the server creates process instances if necessary, and each of them serves many requests until it is finally killed. Therefore, again it is likely that during this life all imports will ultimately be necessary.
source share