Python Packages - Real life example📁
Python Packages - Real life example📁

Python Packages - Real life example📁

CMS Sprint
CMS Unit
Item ID

Link to
Related to Content (1) (Production Pages)
Tags
Type
Learning Item
Lang
EN
Parent item
Sub-item
Sharable link for students

Now that we’ve learned the basics of Python packages, let’s take a real life example.

🌆 The Journey of 'Customer Connect' App

Consider you're building a Customer Relationship Management (CRM) platform - let's call it "Customer Connect 🔗” . In the early stages, all your code might fit in a single file, making it easy to track functions and classes. However, as 'Customer Connect' evolves, managing a single file becomes like navigating a labyrinth. The code becomes messy, hard to understand, and debugging turns into a nightmare.

🔵 Phase 1 - Small multiple files project

To make life easier, you divide your project to multiple Python files handling tasks such as managing user accounts, storing data in JSON format, and sending communication via WhatsApp. Your program is initialized using main.py. So you should have a project file tree that looks like that:

📁 CustomerConnect/
|     main.py
|     manage_users.py
|     storage_json.py
|     communication_whatsapp.py

🟣 Phase 2: The Growth of 'Customer Connect' 🚀

As 'Customer Connect' matures, you decide to add more functionalities. You want to store data in CSV format, communicate via Telegram and Email in addition to WhatsApp. In addition, you now have more functionalities regarding user management. Now, your file tree looks like that:

📁 CustomerConnect/
|     main.py
|     manage_users.py
|     export_users.py
|     analyze_users.py
|     data_report_users.py
|     storage_json.py
|     storage_csv.py
|     communication_whatsapp.py
|     communication_telegram.py
|     communication_email.py

Managing and navigating a growing project can become challenging with a flat file tree. Having all program elements located in one directory can make it tricky to distinguish the relationships between scripts.

Currently the import section in main.py looks like that:

import manage_users
import communication_whatsapp
import communication_telegram
import communication_email
import manage_users
import export_users
import analyze_users
import manage_users

This code lacks any hint of hierarchy and relationship between different modules, and it’s rather long.

🟢 Phase 3: The Transformation of 'Customer Connect' 🦋

To manage the growing complexity of 'Customer Connect', you decide to organize your Python scripts into sub directories. You create a storage directory for all storage-related files; A communication directory for all communication-related files; and a users directory for all users related tasks.

The main directory now only contains main.py . This is how the project’s file tree should look like:

📁 CustomerConnect/
|-----📂 storage/
      |     storage_json.py
      |     storage_csv.py
      |     __init__.py
|-----📂 communication/
      |     whatsapp.py
      |     telegram.py
      |     email.py
      |     __init__.py
|-----📂 users/
      |     manage_users.py
      |     export_users.py
      |     analyze_users.py
      |     __init__.py
|---- main.py

Each of these newly created directories is called a Python Package.

Importing modules from packages

When we import modules inside packages, we can use the dot notation, like that:

import storage.storage_json
import communication.whatsapp

However, in order to avoid lengthy module names, it is advisable to use the from syntax:

from communication import whatsapp

That’s probably pretty straightforward, but you might have noticed a new addition to each directory: an __init.py__ file. More about this in the upcoming chapter.