- Automate Python tasks using command-line scripts.
- Use pip to install and manage external packages.
- Write modular Python scripts with clean entry points.
- Track dependencies using a requirements.txt file.
- Generate structured outputs using file I/O techniques.
In this lab, you will build a Python automation tool that uses pip-installed packages and scriptable logic to automate a real-world task. Your script will:
- Use pip to install third-party packages (e.g.,
requests). - Fetch or process external data.
- Write structured output to a local file.
- Track all dependencies in
requirements.txtfor reproducibility.
This lab emphasizes automation, scripting practices, and environment management using the standard Python ecosystem.
- Go to the provided GitHub repository link.
- Fork the repository to your GitHub account.
- Clone the forked repository to your local machine using:
git clone <repo-url>
cd module-lab-pip-pypi-scriptingEnsure Python and pip are installed:
python --version
pip --versionOptionally, create a virtual environment:
python -m venv venv
source venv/bin/activate # macOS/Linux
venv\Scripts\activate # WindowsInstall any required dependencies:
pip install -r requirements.txtYour goal is to create a Python script that automates a small task:
- Uses one or more pip-installed packages (e.g.,
requests,pandas,rich) - Outputs data to a
.txtor.csvfile using File I/O - Logs or prints messages to confirm behavior
- Is executable from the command line
- Records dependencies in
requirements.txt
You will implement a script with the following design principles:
- Use
pipto install packages - Import modules inside a Python script
- Wrap logic in
if __name__ == "__main__"to support reusability - Structure output files with filenames that include timestamps
- Track dependencies using
pip freeze > requirements.txt
from datetime import datetime
log_data = ["User logged in", "User updated profile", "Report exported"]
filename = f"log_{datetime.now().strftime('%Y%m%d')}.txt"
with open(filename, "w") as file:
for entry in log_data:
file.write(f"{entry}\n")
print(f"Log written to {filename}")import requests
def fetch_data():
response = requests.get("https://jsonplaceholder.typicode.com/posts/1")
if response.status_code == 200:
return response.json()
return {}
if __name__ == "__main__":
post = fetch_data()
print("Fetched Post Title:", post.get("title", "No title found"))After installing any packages with pip install ..., run:
pip freeze > requirements.txt- Use clear function names (
fetch_data,write_log) for clarity. - Always check file write success with print or logging statements.
- Avoid hardcoding data—use variables and functions where appropriate.
- Use virtual environments to isolate dependencies.
- Wrap script logic in
if __name__ == "__main__"for script reusability.
After completing this lab, you will:
✅ Automate tasks with Python scripting
✅ Use external packages from PyPi with pip
✅ Track project dependencies with requirements.txt
✅ Generate structured output files from your script
✅ Structure projects for portability and collaboration
These scripting and packaging skills are essential for building automation tools and working in modern Python development workflows.