|
| 1 | + |
| 2 | +# Module Lab: Automating Python Projects with Pip, PyPi & Scripting |
| 3 | + |
| 4 | +## Learning Goals |
| 5 | + |
| 6 | +- Automate Python tasks using command-line scripts. |
| 7 | +- Use pip to install and manage external packages. |
| 8 | +- Write modular Python scripts with clean entry points. |
| 9 | +- Track dependencies using a requirements.txt file. |
| 10 | +- Generate structured outputs using file I/O techniques. |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +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: |
| 15 | + |
| 16 | +- Use pip to install third-party packages (e.g., `requests`). |
| 17 | +- Fetch or process external data. |
| 18 | +- Write structured output to a local file. |
| 19 | +- Track all dependencies in `requirements.txt` for reproducibility. |
| 20 | + |
| 21 | +This lab emphasizes automation, scripting practices, and environment management using the standard Python ecosystem. |
| 22 | + |
| 23 | +## Setup Instructions |
| 24 | + |
| 25 | +### Fork and Clone the Repository |
| 26 | + |
| 27 | +1. Go to the provided GitHub repository link. |
| 28 | +2. Fork the repository to your GitHub account. |
| 29 | +3. Clone the forked repository to your local machine using: |
| 30 | + |
| 31 | +```bash |
| 32 | +git clone <repo-url> |
| 33 | +cd module-lab-pip-pypi-scripting |
| 34 | +``` |
| 35 | + |
| 36 | +### Install Python and pip |
| 37 | + |
| 38 | +Ensure Python and pip are installed: |
| 39 | + |
| 40 | +```bash |
| 41 | +python --version |
| 42 | +pip --version |
| 43 | +``` |
| 44 | + |
| 45 | +Optionally, create a virtual environment: |
| 46 | + |
| 47 | +```bash |
| 48 | +python -m venv venv |
| 49 | +source venv/bin/activate # macOS/Linux |
| 50 | +venv\Scripts\activate # Windows |
| 51 | +``` |
| 52 | + |
| 53 | +Install any required dependencies: |
| 54 | + |
| 55 | +```bash |
| 56 | +pip install -r requirements.txt |
| 57 | +``` |
| 58 | + |
| 59 | +## Tasks |
| 60 | + |
| 61 | +### Task 1: Define the Problem |
| 62 | + |
| 63 | +Your goal is to create a **Python script** that automates a small task: |
| 64 | + |
| 65 | +- Uses one or more pip-installed packages (e.g., `requests`, `pandas`, `rich`) |
| 66 | +- Outputs data to a `.txt` or `.csv` file using File I/O |
| 67 | +- Logs or prints messages to confirm behavior |
| 68 | +- Is executable from the command line |
| 69 | +- Records dependencies in `requirements.txt` |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +### Task 2: Determine the Design |
| 74 | + |
| 75 | +You will implement a script with the following design principles: |
| 76 | + |
| 77 | +- Use `pip` to install packages |
| 78 | +- Import modules inside a Python script |
| 79 | +- Wrap logic in `if __name__ == "__main__"` to support reusability |
| 80 | +- Structure output files with filenames that include timestamps |
| 81 | +- Track dependencies using `pip freeze > requirements.txt` |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +### Task 3: Develop and Run Your Script |
| 86 | + |
| 87 | +#### Step 1: Create a script called `generate_log.py` |
| 88 | + |
| 89 | +```python |
| 90 | +from datetime import datetime |
| 91 | + |
| 92 | +log_data = ["User logged in", "User updated profile", "Report exported"] |
| 93 | +filename = f"log_{datetime.now().strftime('%Y%m%d')}.txt" |
| 94 | + |
| 95 | +with open(filename, "w") as file: |
| 96 | + for entry in log_data: |
| 97 | + file.write(f"{entry}\n") |
| 98 | + |
| 99 | +print(f"Log written to {filename}") |
| 100 | +``` |
| 101 | + |
| 102 | +#### Step 2: Add an API integration using `requests` |
| 103 | + |
| 104 | +```python |
| 105 | +import requests |
| 106 | + |
| 107 | +def fetch_data(): |
| 108 | + response = requests.get("https://jsonplaceholder.typicode.com/posts/1") |
| 109 | + if response.status_code == 200: |
| 110 | + return response.json() |
| 111 | + return {} |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + post = fetch_data() |
| 115 | + print("Fetched Post Title:", post.get("title", "No title found")) |
| 116 | +``` |
| 117 | + |
| 118 | +#### Step 3: Track your dependencies |
| 119 | + |
| 120 | +After installing any packages with `pip install ...`, run: |
| 121 | + |
| 122 | +```bash |
| 123 | +pip freeze > requirements.txt |
| 124 | +``` |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## Best Practices |
| 129 | + |
| 130 | +- Use clear function names (`fetch_data`, `write_log`) for clarity. |
| 131 | +- Always check file write success with print or logging statements. |
| 132 | +- Avoid hardcoding data—use variables and functions where appropriate. |
| 133 | +- Use virtual environments to isolate dependencies. |
| 134 | +- Wrap script logic in `if __name__ == "__main__"` for script reusability. |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## Conclusion |
| 139 | + |
| 140 | +After completing this lab, you will: |
| 141 | + |
| 142 | +✅ Automate tasks with Python scripting |
| 143 | +✅ Use external packages from PyPi with pip |
| 144 | +✅ Track project dependencies with `requirements.txt` |
| 145 | +✅ Generate structured output files from your script |
| 146 | +✅ Structure projects for portability and collaboration |
| 147 | + |
| 148 | +These scripting and packaging skills are essential for building automation tools and working in modern Python development workflows. |
0 commit comments