What is the difference between a module and a script in Python?

A script is generally a directly executable piece of code, run by itself. A module is generally a library, imported by other pieces of code.

Note that there’s no internal distinction — both are executable and importable, although library code often won’t do anything (or will just run its unit tests) when executed directly and importing code designed to be a script will cause it to execute, hence the common if __name__ == "__main__" test.

Leave a Comment