
PYTHON — Python Scripting- Modules and Packages
The computer was born to solve problems that did not exist before. — Bill Gates

PYTHON — Timestamp Formatting in Python
Python Scripting: Modules and Packages
In Python, there are several terms that you may encounter, such as scripts, modules, packages, and libraries. Understanding the differences between these terms is crucial for writing efficient and organized code.
Scripts
A Python script is a file that is meant to be run directly. When executed, it should perform a specific task. Typically, scripts contain code that is written outside the scope of any classes or functions. Below is an example of a simple Python script:
# script.py
def main():
print("Hello, World!")
if __name__ == "__main__":
main()To run this script, you can execute it from the command line using python script.py. Here, the if __name__ == "__main__": block ensures that the main function is executed when the script is run directly.
Modules
A Python module is a file that is intended to be imported into other scripts or modules. It often contains members like classes, functions, and variables that can be used in other files. Consider the following example of a Python module:
# module.py
def add(a, b):
return a + bThis module can be imported and used in another script like this:
# main.py
import module
result = module.add(3, 4)
print(result) # Output: 7Packages
A package in Python is a collection of related modules that work together to provide specific functionality. These modules are stored within a folder and can be imported just like any other modules. The folder containing the modules often includes a special __init__.py file, which indicates that it's a package. Here's an example of a package structure:
my_package/
__init__.py
module1.py
module2.pyIn this example, my_package is a package containing module1 and module2, which can be imported into other scripts or modules.
Libraries
The term “library” in Python loosely refers to a bundle of code, typically containing multiple modules that offer a wide range of functionality. For instance, Matplotlib is a popular plotting library, and the Python Standard Library contains hundreds of modules for various common tasks.
The Python Standard Library comes bundled with the Python installation, allowing you to use its modules without downloading them from external sources. For example, the datetime module is part of the Python Standard Library and can be imported directly into your scripts.
In summary, understanding these terms is essential for structuring your Python code effectively. By organizing your code into scripts, modules, and packages, you can create scalable and maintainable Python projects.
In the next section, we’ll explore how to handle situations where Python cannot find a module you’ve installed, and we’ll delve into using Python’s package manager to install and manage packages.

