Python: package

Python package is a collection of modules in a directory. To tell Python, that this directory should be considered a package, we have to add a file named __init__.py

Python package is a directory that contains a __init__.py file and a set of modules.

The name of the directory is considered as the name of the package.

Create Package

For example, if we want to create a package for an e-commerce application, we can create in the following directory structure-

Project Root/
    |
    |- ecom/
    |   |- __init__.py
    |   |- category.py
    |   |- image.py
    |   |- product.py
    |   |
    |   |- order/
    |   |   |- __init__.py
    |   |   |- history.py
    |   |   |- process.py
    |   |   |- reorder.py
    |   |
    |   |- payment/
    |   |   |- __init__.py
    |   |   |- cod.py
    |   |   |- crypto.py
    |   |   |- paypal.py
    |   |   |- stripe.py
    |   |   |
    |   |   |- refund/
    |   |   |   |- __init__.py
    |   |   |   |- refund.py
    |   |   |   |- verify.py
Plaintext

Here we have a parent package named “ecom”, as there is an __init__.py file in the directory. The name of the package is “ecmo” as that is the directory name.

Inside the “ecom” package, there are 2 more packages named “order” and “payment”, which provide functionality for the relevant operation.

Create a directory named “ecom”.
In the directory create a file named “__init__.py”.
Create another file named “product.py”.
Add the following lines in the “product.py”.
# ecom/product.py


class Product:
    def __init__(self) -> None:
        print("Product initialized")
Python

Similarly, create other files mentioned in the directory structure above.

Import from Package

To import the module from “product.py” we can do it in multiple ways-

import ecom.product

my_product = ecom.product.Product()
Python

We can set an alias to the imported package. Like, here we have named our ecom.product as ep, so for the rest of the module we can use ep.<some_property> in place of product.<some_property>

import ecom.product as ep

my_product = ep.Product()
Python

We can import multiple packages separated by command. like, we have imported product and payment from ecom here.

from ecom import product, payment

my_product = product.Product()
paypal = payment.paypal.Paypal()
Python

We can import any specific item from a module inside a package. Like, here we have imported the Product class, from ecom.product.

from ecom.product import Product

my_product = Product()
Python

Relative Importing

Sometimes if we want to ignore a long package path while importing and the package is close, then we might use a relative import.

  • Single dot(.) – for modules in the same directory
  • Two dots(..) – for modules that are in the parent directory(relative to from where we are importing).
  • Three dots(…) – for modules that are 2 steps back, in the parent of the parent directory(relative to from where we are importing).
# ecom/payment/process.py

from .paypal import Paypal # Import from the same directory
from ..product import Product # Import from 1 step back
from ...product import Product # Import from 2 step back
Python

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.