Python Packages

📦 Python Packages

A package in Python is a collection of modules stored inside a folder.
Packages help in:

 Organizing large programs
Reusing code
 Structuring project files properly


🧩 Difference Between Module & Package

FeatureModulePackage
DefinitionA single .py fileA folder containing multiple modules
Examplemath.pyrequests, numpy
ContainsFunctions, variables, classesModules, sub-packages

📁 Package Structure Example

Create a folder named:

my_package/
__init__.py
greetings.py
math_tools.py

greetings.py

def say_hello(name):
return f"Hello {name}!"

math_tools.py

def add(a, b):
return a + b

__init__.py

from .greetings import say_hello
from .math_tools import add

 Using the Package

Create another Python file:


 


 Importing Specific Modules from a Package

from my_package.math_tools import add

print(add(5, 7))


🏷 What is __init__.py?

  • A special file that tells Python this folder is a package

  • Can be empty or contain initialization code

  • Used to control what gets imported when using:

from my_package import *

🔥 Built-in Python Packages

Python already includes many packages:

PackageUse
jsonJSON parsing
urllibURL handling
emailSending/receiving emails
tkinterGUI apps
collectionsAdvanced data structures

Example:



⬇ Installing External Packages Using pip

To install:

pip install numpy

To use:

import numpy as np
print(np.array([1, 2, 3]))

 Uninstall a Package

pip uninstall numpy

 Check Installed Packages

pip list

 Create a Custom Package and Install It

  1. Create folder structure:

my_utils/
__init__.py
tools.py

tools.py:

def welcome():
print("Welcome to My Utils!")
  1. Install locally (inside folder):

pip install .
  1. Now use anywhere:

import my_utils

my_utils.welcome()


📌 Summary

FeatureExample
Create packageFolder with __init__.py
Import packageimport my_package
Import modulefrom my_package import module
Install external packagepip install modulename

🧠 Practice Tasks

 Create a package named school with modules:

  • students.py (store student list)

  • grades.py (function to calculate percentage)

 Install and use the pyjokes or emoji package.

 Create a package and import it using alias.

You may also like...