Understanding Python’s code structure is essential for writing reusable, maintainable, and executable programs. In this guide, we’ll explore the differences between scripts and modules, how to use the shebang line (#!/usr/bin/env python3
), and best practices for organizing and executing Python code.
Scripts vs. Modules in Python
1. Scripts: Executable Files
A script is a Python file (.py
) designed to be executed directly. Scripts typically perform tasks like data processing, automation, or running applications.
#!/usr/bin/env python3
# This is a script named "greet.py"
print("Hello, World!")
2. Modules: Reusable Code Libraries
A module is a Python file intended to be imported and reused in other scripts or modules. Modules contain functions, classes, or variables.
# This is a module named "math_utils.py"
def add(a, b):
return a + b
def subtract(a, b):
return a - b
Shebang Line and File Permissions
Shebang Line: #!/usr/bin/env python3
The shebang line tells the operating system which interpreter to use to execute the script. Place it at the top of your script:
#!/usr/bin/env python3
print("This script uses Python 3!")
Setting File Permissions
To run a script directly, you must make it executable using chmod
:
chmod +x script.py # Grant execute permission
./script.py # Run the script
Example: Script Execution Workflow
- Create a script:
greet.py
- Add the shebang line and code:
#!/usr/bin/env python3
name = input("Enter your name: ")
print(f"Hello, {name}!")
- Make it executable:
chmod +x greet.py
- Run it:
./greet.py
Practice Work
Exercise 1: Create a Script
Write a script called calculator.py
that asks the user for two numbers and prints their sum. Include the shebang line and make it executable.
#!/usr/bin/env python3
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print(f"Sum: {num1 + num2}")
Exercise 2: Convert Script to Module
Convert the calculator.py
script into a reusable module with functions for addition, subtraction, multiplication, and division.
# Module: calculator.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
Exercise 3: Fix Permissions
If a script returns a “Permission denied” error, what command would you use to fix it? Write the answer below.
# Answer:
chmod +x script_name.py
Best Practices
- Use Shebang for Clarity: Always include
#!/usr/bin/env python3
in scripts to specify the Python version. - Organize Code into Modules: Reuse functions/classes across projects by separating logic into modules.
- Name Scripts and Modules Clearly: Use descriptive filenames like
data_analysis.py
orutils.py
.
Conclusion
Mastering Python’s code structure—scripts, modules, and execution—is key to building scalable and maintainable applications. Practice creating scripts, converting them to modules, and using the shebang line to ensure your code runs smoothly across systems.
Need more Python tutorials? Check out our posts on Python Functions and Object-Oriented Programming in Python.