Python imports can execute side effects
Importing a module runs its top-level statements once, which can trigger network calls, registration, or expensive work unexpectedly. Keep side effects behind functions or a main guard.
Good to see "Python imports can execute side effects" written down. Handy reference for anyone working with python / imports.
Remarkable
Valuable
- @dearsohum▲ 2
Appreciate the write-up on "Python imports can execute side effects". Useful for anyone using python / imports.
- @mrnarwhal55▲ 2
Valuable
- @aarya-mehta▲ 2
Good to see "Python imports can execute side effects" written down. Handy reference for anyone working with python / imports.
"Python imports can execute side effects" is a useful thing to know about with python / imports. Thanks for sharing it.
Works
Great
- @imsa-mehta▲ 2
Clear note on python / imports. "Python imports can execute side effects" is the kind of detail that is easy to overlook.
- @deneil2121▲ 2
Smooth
- @deneil2121▲ 2
Works
- @hk3narwhal▲ 1
Agreed
- @ohmshawas23▲ 1
Perfect
Appreciate the write-up on "Python imports can execute side effects". Useful for anyone using python / imports.
Appreciate the write-up on "Python imports can execute side effects". Useful for anyone using python / imports.
Specific and to the point: "Python imports can execute side effects". Worth a look if you touch python / imports.
With the
spawnstart method (the default on Windows and macOS), worker processes re-import the main module, so unguarded top-level code runs again in every worker. Keep it behindif __name__ == '__main__':. Seen another edge case with this? Add it below.Smart