Avatar ·

Code execution when importing a module in Python is a bad practice?

📁 question, AI

Several times I've encountered mentions that executing code when importing a module is bad practice. At the same time, I couldn't find any explanations for this statement anywhere. There are two questions.

First: Why is executing code when importing a module considered bad practice?

Second: What constitutes code execution in the context of this statement? Assigning values to global variables, executing functions, or something else? After all, defining classes and functions in this module, as well as importing other modules (in the module we are importing), could also be considered "code"?