0% found this document useful (0 votes)
6 views18 pages

Understanding Python Modules and Packages

The document explains Python modules, which are files containing Python code that help organize and reuse code efficiently. It covers types of modules (built-in, user-defined, and external), how to import them, and the module search path. Additionally, it introduces Python packages as directories containing modules and the significance of the __init__.py file.

Uploaded by

samikshakohli93
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views18 pages

Understanding Python Modules and Packages

The document explains Python modules, which are files containing Python code that help organize and reuse code efficiently. It covers types of modules (built-in, user-defined, and external), how to import them, and the module search path. Additionally, it introduces Python packages as directories containing modules and the significance of the __init__.py file.

Uploaded by

samikshakohli93
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON MODULES

By: Samiksha
DEFINITION
1.A module in python is a file containing python code - definitions of functions, classes and
variables, and even executable code.
[Link] help organize and reuse code efficiently by splitting large programs into smaller,
manageable parts.
3.A module can be:
A built - in module (like math, os, sys, etc.)
A user defined module (a .py file created by you)
A third - party module (installed using pip, like requests, numpy)
TYPES

math NumPy (numerical)


pandas (data analysis)
datetime USER- Flask/Django (web dev)
os BUILT - IN EXTERNAL SciPy (scientific and technical
DEFINED
sys computing)
random Seaborn (data visualization)

a .py file created by you

No installation needed installation needed using pip install library_name


HOW TO IMPORT MODULES
You can import modules in several ways:

Method Example Description

import module_name Imports the entire module.


import math

import module_name as
Import with an abbreviation.
abbreviation import numpy as np

from module_name import function from math import sqrt Import specific items.
BUILT-IN MODULE
import math:
This imports Python's built-in math module, which provides access to mathematical functions like
square roots, trigonometry, logarithms, etc.

OUTPUT:
USER-DEFINED MODULE
You created 2 functions:

say_hello(name) prints Hello, [name]!

say_bye(name) prints Bye, [name]!
These functions don’t run by themselves. You will use them in another file.
You used import mymodule to bring in the other file.
Then you called the functions using:

mymodule.say_hello('World') prints Hello, World!

mymodule.say_bye('World') prints Bye, World!

OUTPUT:
EXTERNAL MODULE
from cs50 import get_string
is used in Python programs that rely on the CS50 library — a library developed for
the Harvard CS50 course.

OUTPUT:
EXAMPLE

OUTPUT:
MODULE SEARCH PATH
[Link] you use import module_name, Python needs to find that module on your
computer.
[Link] searches for the module in a few specific loctions, in this order:
The current folder (where your program is saved).
The folders listed in an environment variable called PYTHONPATH.
The standard Python library folders.
The folders where installed packages are kept (site - packages).
EXAMPLE

• import sys imports Python’s sys module, which gives access to system-related info.

• [Link] is a list of folders where Python looks for modules when you use import.

• print([Link]) prints that list.

OUTPUT:
This list shows all the places Python looks when you try to
import something.
If your module is not in one of these folders, Python will give an
error:
ModuleNotFoundError
RELOADING A MODULE
If you change the code inside a module (like adding or editing a function) while python is still
running, Python will automatically reload the updated version.

Reloading is like refreshing your module - Python reads the updated file again so your new
code works immediately.

OUTPUT:
PYTHON PACKAGES
DEFINITION
A Python package is essentially a directory containing one or more Python modules
(.py files) and, crucially, a special file named __init__.py.
The presence of the __init__.py file (even if empty) signals to Python that the
directory should be treated as a package, allowing its modules and sub-packages to
be imported.
EXAMPLE
__init__.py - This file tells Python: "This is a package"
[Link] - A module inside the package
[Link] - Another module

SYNTAX:

Specific functions or classes from a module can be imported directly:


PROGRAM

[Link]

[Link]

__init__.py
[Link]

Output:
THANK YOU

You might also like