Modules, Packages, and PIP
Overview and Objectives
In this lab you will explore Python’s modular capabilities. You will:
- Learn to import and use built‐in modules (such as
math
,random
, andplatform
) to perform common tasks. - Create your own module (with functions like
greet
andfactorial
), organize it into a package, and import it into another script. - Use PIP to install an external package (e.g.,
colorama
orrequests
) and write a small program that demonstrates its functionality.
By the end of this lab, you will be comfortable with module imports, package organization, and external library usage.
Instructions and Tasks
Task 1: Using Built-in Modules
- Write a Python script (e.g.,
builtin_usage.py
) that:- Imports the
math
,random
, andplatform
modules. - Generates a random integer between 1 and 100 using the
random
module. - Calculates the square root of that number using
math.sqrt
and rounds it down (usingmath.floor
or by converting to an integer). - Retrieves and prints the system’s OS name and Python version using functions from the
platform
module.
- Imports the
- Your program should print descriptive messages for each output (e.g., “Random Number:”, “Square Root (floored):”, “Operating System:”, “Python Version:”).
Task 2: Creating and Importing a Custom Module
- Create a new Python file named
utilities.py
that defines at least the following functions:greet(name)
: Returns a greeting string for the given name.factorial(n)
: Returns the factorial ofn
(you may use functions from themath
module if needed).
- Organize
utilities.py
into a package:- Create a folder named
mypackage
and moveutilities.py
inside it. - Include an empty
__init__.py
file in themypackage
folder.
- Create a folder named
- Write another script (e.g.,
use_utilities.py
) that:- Imports your custom module using the package name (e.g.,
from mypackage import utilities
). - Calls both
greet
andfactorial
functions and prints their outputs.
- Imports your custom module using the package name (e.g.,
Task 3: Using PIP and an External Package
- Choose a third-party package (for example,
colorama
orrequests
). - Using PIP, install the selected package.
- Write a script (e.g.,
external_package.py
) that:- Imports the package.
- Demonstrates a simple usage of the package. For instance, if using
colorama
, print a colored text message; if usingrequests
, fetch a URL and print the HTTP status code.
- Include a comment at the top of your script explaining that the package must be installed (e.g.,
# Requires colorama; install with: pip install colorama
).
Submission
Submit the following files via a link to a single GitHub repository containing all the files.:
builtin_usage.py
mypackage/utilities.py
(with an accompanying empty__init__.py
)use_utilities.py
external_package.py
Include example outputs (screenshots or text logs) demonstrating that each script works as intended.
Grading Rubric
Criteria | Points | Description |
---|---|---|
Use of Built-in Modules | 10 pts | Program correctly imports and utilizes math , random , and platform modules. Outputs include a random integer, its floored square root, and system info. |
Custom Module Creation and Usage | 15 pts | A utilities.py module is created with greet and factorial functions. The main script imports it using the package structure and calls both functions correctly. |
Package Organization | 5 pts | The custom module is correctly organized in a package (folder with __init__.py ), and imported using the package name. |
PIP Package Installation and Usage | 10 pts | An external package is installed and successfully used in a script (e.g., printing colored text or performing an HTTP request) with correct and clear output. |
Code Quality and Clarity | 5 pts | Code is well-organized, properly commented, and outputs are clearly labeled. |
Total Points: 40 points
Partial credit will be awarded for submissions that demonstrate a correct understanding of the concepts and correct implementation of some, but not all, required elements. If a solution is incomplete, contains minor errors, or omits one or more components of a task, evaluators may award partial points proportional to the work correctly completed. Points may be deducted for errors in logic, formatting, or missing features, but credit will be given where the underlying approach is sound and the code clearly shows an attempt to meet the rubric criteria.