As Python applications grow in complexity, organizing code becomes essential. Python supports this through packages, which are a way of structuring Pythonβs module namespace by using βdotted module names.β Packages help group related modules together into a hierarchical structure and enable modular development, better maintainability, and code reuse.
A Python package is a directory that contains multiple related Python modules and includes a special file named __init__.py. This file tells Python that the directory should be treated as a package.
my_package/
This file marks the directory as a package. It can be empty or include initialization code for the package.
my_package/
βββ __init__.py
Create Python files (modules) inside the package directory.
my_package/
βββ __init__.py
βββ math_ops.py
βββ string_ops.py
def add(a, b):
return a + b
def multiply(a, b):
return a * b
def capitalize_words(text):
return " ".join(word.capitalize() for word in text.split())
from my_package import math_ops
print(math_ops.add(2, 3))
from my_package.math_ops import multiply
print(multiply(3, 4))
import my_package.string_ops as str_ops
print(str_ops.capitalize_words("hello world"))
The __init__.py file allows you to define what is available when the package is imported. It can include imports, variables, or initialization logic.
# my_package/__init__.py
from .math_ops import add
from .string_ops import capitalize_words
from my_package import add, capitalize_words
Packages can be nested to create sub-packages, allowing for deeper modular hierarchies.
my_package/
βββ __init__.py
βββ math/
β βββ __init__.py
β βββ arithmetic.py
βββ strings/
β βββ __init__.py
β βββ formatting.py
from my_package.math.arithmetic import add
from my_package.math_ops import add
from .math_ops import add
from my_package import add, capitalize_words
print(add(10, 20))
print(capitalize_words("hello from python"))
You can distribute your package using setuptools and upload it to PyPI for others to install via pip.
my_package_project/ βββ my_package/ β βββ __init__.py β βββ math_ops.py β βββ string_ops.py βββ setup.py βββ README.md
from setuptools import setup, find_packages
setup(
name="my_package",
version="0.1",
packages=find_packages(),
description="A sample Python package",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
author="Your Name",
author_email="your.email@example.com",
url="https://github.com/yourusername/my_package",
classifiers=[
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
pip install setuptools wheel twine
python setup.py sdist bdist_wheel
twine upload dist/*
Once uploaded, others can install it using pip:
pip install my_package
def add(a: int, b: int) -> int:
return a + b
"""
math_ops.py - Mathematical operations
"""
def add(a, b):
"""Returns the sum of a and b"""
return a + b
ecommerce_app/ βββ ecommerce/ β βββ __init__.py β βββ cart.py β βββ payment.py β βββ product.py βββ tests/ β βββ test_cart.py β βββ test_payment.py βββ setup.py βββ README.md
from ecommerce.cart import add_to_cart
from ecommerce.payment import process_payment
Creating packages in Python is essential for building scalable, maintainable, and reusable code. Whether you are building a library for public distribution or structuring a large internal project, packages help enforce organization and encapsulation. With a solid understanding of directories, __init__.py, importing, and distribution tools, you can start creating and managing professional-grade Python packages effectively.
Start small, structure your projects with a package mindset from the beginning, and gradually adopt best practices like testing, documentation, and distribution. Packages are the building blocks of Python's vast ecosystemβand with this knowledge, you can contribute to it confidently.
Python is commonly used for developing websites and software, task automation, data analysis, and data visualisation. Since it's relatively easy to learn, Python has been adopted by many non-programmers, such as accountants and scientists, for a variety of everyday tasks, like organising finances.
Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works.
The point is that Java is more complicated to learn than Python. It doesn't matter the order. You will have to do some things in Java that you don't in Python. The general programming skills you learn from using either language will transfer to another.
Read on for tips on how to maximize your learning. In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.
6 Top Tips for Learning Python
The following is a step-by-step guide for beginners interested in learning Python using Windows.
Best YouTube Channels to Learn Python
Write your first Python programStart by writing a simple Python program, such as a classic "Hello, World!" script. This process will help you understand the syntax and structure of Python code.
The average salary for Python Developer is βΉ5,55,000 per year in the India. The average additional cash compensation for a Python Developer is within a range from βΉ3,000 - βΉ1,20,000.
Copyrights © 2024 letsupdateskills All rights reserved