Does Python import order matter

I read here about sorting your import statements in Python, but what if the thing you import needs dependencies that have not yet been imported? Is this the difference between compiled languages ​​and is interpreted? I am proceeding from a JavaScript background and the loading order of your scripts, while Python does not seem to care. Thank you

+5
source share
3 answers

No, this is not the case, because each python module must be standalone and import everything it needs. This is true for importing entire modules and only certain parts of it.

+5
source

The import order does not matter. If a module uses other modules, it must import them. Python treats each .py file as a standalone unit, as far as this file can be seen.

(Technically, changing the import order can change the behavior, because modules can have an initialization code that runs when they are first imported. If this initialization code has side effects, it is possible for the modules to have interactions with each other. However, this will be a design error in these modules. The import order should not matter, so the initialization code must also be written so as not to depend on any particular order.)

+13
source

The import order of Python does not matter when you import the standard python libraries / modules. But order matters for local import of applications / libraries, since you can get stuck in a loop of dependencies, so look before importing.

+1
source

Source: https://habr.com/ru/post/1206436/


All Articles