fbpx

A Beginner's Guide to Modules and Packages in Python Programming

Python


INTRODUCTION

Discover the fundamental differences between Python modules and packages in this detailed guide. Learn how modules—single Python files containing reusable code—compare to packages, which organize multiple modules into structured directories. Understand how to create, import, and use both modules and packages to streamline your Python projects. Explore practical examples, from creating basic modules to leveraging Python’s built-in sys.path for locating and managing dependencies. Whether you’re a beginner or an experienced developer, this blog will help you master modular programming in Python for better organization, reusability, and scalability.

Key Differences Between Modules and Packages

Aspect

Module

Package

Definition

A single Python file with code (e.g., .py file).

 organized in a directory.

Structure

A single file.

multiple modules and as well as __init__.py file.

Purpose

Organizes related code into a single file.

Organizes related modules into a directory structure.

Importing

Imported using import module_name.

Imported using import package_name.module_name.

Example

math.py, greetings.py

my_package/greetings.py, my_package/farewell.py

__init__.py File

Not required.

 directory as a package.

Python Module:

—————————

 A module is a single file that contains Python code, such as functions, variables, and classes.

It can also contain runnable code.

 The primary purpose of a module is to break down large codebases into smaller, manageable, and reusable components.

Creating a python module:

————————————–

Mynew.py  file

——————-

def greet(name)

return name;

def add(x,y)

return (x+y)

 

import a new module

——————————-

calculator.py

import mynew

print(mynew.greet(“iies”));

print(mynew.add(5,10))

From statements in Python:

————————————–

# importing sqrt() and factorial from the

# module math

from math import sqrt, factorial

# math.sqrt(16) and math.factorial()

# are required.

print(sqrt(16))

print(factorial(6))

from math import sqrt

printf(sqrt(16))

Import all Names

———————-

# importing sqrt() and factorial from the

# module math

from math import *

 

# math.sqrt(16) and math.factorial()

# are required.

print(sqrt(16))

print(factorial(6))

How Python Locates Modules:

  1. Standard Library Directories: Python first looks in its built-in standard library, which contains pre-installed modules like os, math, sys, etc.
  2. Current Working Directory: Python checks the directory where the script is running. Environment Variables: Python checks directories listed in the PYTHONPATH environment variable for additional modules.
  3. Installed Packages: Python also looks in directories where external packages have been installed (e.g., site-packages directory, which is used by pip to install modules).
  4. Module Search Path: The directories Python checks for modules are stored in the path list, which includes the above locations.

Example of Locating a Module:

import sysprint(sys.path)

  • Standard Library Directories

Python first looks in its built-in standard library. For example, if you import the math module, Python will look in the standard library where math.py is located.

import mathprint(math.sqrt(16))  # Uses the math module from the standard library

  • Current Working Directory

If a module is in the same directory as the script, Python will find it there.

Example:                                                 

Suppose you have a file mymodule.py in the same directory as your script main.py:

# mymodule.pydef greet(name):    return f”Hello, {name}!” 

  • After installing the requests package using pip:

pip install requests

You can import it like this:

import requestsresponse = requests.get(‘https://www.example.com’)print(response.status_code)

Python will locate the requests module in the site-packages directory (e.g., /usr/lib/python3.9/site-packages/ on Linux) and allow you to use it.

    5. Module Search Path (sys.path)

Python maintains a list of directories that it searches for modules, which is stored in sys.path. You can inspect this list to see where Python will search for modules.

Example:

 import sysprint(sys.path)