The difference between modules, scripts, and programs

We're sometimes asked about the differences between Python modules, Python scripts, and Python programs. Any .py file constitutes a Python module, but as we have seen modules can be written for convenient import, convenient execution, or, using the if __name__ == "__main__" idiom, both.

We strongly recommend making even simple scripts importable since it eases development and testing so much if you can access your code from the Python REPL. Likewise, even modules which are only ever meant to be imported in production settings benefit from having executable test code. For this reason nearly all modules we create have this form of defining one or more importable functions with a postscript to facilitate execution.

Whether you consider a module to be a Python script or Python program is a matter of context and usage. It's certainly wrong to consider Python to be merely a scripting tool — in the vein of Windows batch files or Unix shell scripts — as many large and complex applications have been built exclusively with Python.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset