Skip to main content

Command Palette

Search for a command to run...

05 - Modules, Packages and PIP

Published
5 min read
R

I am an aspiring DevOps Engineer proficient with containers and container orchestration tools like Docker, Kubernetes along with experienced in Infrastructure as code tools and Configuration as code tools, Terraform, Ansible. Well-versed in CICD tool - Jenkins. Have hands-on experience with various AWS and Azure services. I really enjoy learning new things and connecting with people across a range of industries, so don't hesitate to reach out if you'd like to get in touch.

Modules

  • A module in Python is a file containing Python code (functions, classes, or variables) that can be reused in other Python programs.

  • Modules help organize code into manageable and reusable components, promoting modular programming and reducing redundancy.

Types of Modules in Python

  1. Built-in Modules: Pre-installed modules that come with Python (e.g., math, os, sys).

  2. User-defined Modules: Python files we create containing reusable code.

  3. External Modules: Third-party modules that can be installed using package managers like pip (e.g., requests, numpy).

Importing Modules

  • To use a module, we can import it using the import keyword.

Basic Import

import math
print(math.sqrt(16))  # Output: 4.0

Import Specific Functions or Variables

from math import sqrt, pi
print(sqrt(25))  # Output: 5.0
print(pi)        # Output: 3.141592653589793

Import with Alias

import math as m
print(m.sqrt(36))  # Output: 6.0
from math import *
print(sqrt(49))  # Output: 7.0

Finding Module Attributes

  • Use the dir() function to list all attributes (functions, classes, variables) of a module.

      import math
      print(dir(math))
    

Built-in Modules Examples

  1. math Module: Provides mathematical functions.

     import math
     print(math.factorial(5))  # Output: 120
    
  2. os Module: Interacts with the operating system.

     import os
     print(os.getcwd())  # Output: Current working directory
    
  3. random Module: Generates random numbers.

     import random
     print(random.randint(1, 10))  # Output: Random number between 1 and 10
    
  4. datetime Module: Handles date and time.

     from datetime import datetime
     print(datetime.now())  # Output: Current date and time
    

sys.path in Python

  • sys.path is a list in the sys module that contains the directories Python searches when looking for a module to import.

  • It defines the module search path and allows to add or modify directories where Python looks for modules.

  • When we import a module in Python, the interpreter searches for it in the directories listed in sys.path. If the module isn't found in any of these directories, Python raises a ModuleNotFoundError.

Adding Directories to sys.path

  1. Temporarily Adding a Path

    • We can append a directory to sys.path during runtime. This change is temporary and lasts only for the duration of the script execution.

        import sys
      
        # Add a directory to the module search path
        sys.path.append("/path/to/your/directory")
      
        # Now you can import modules from that directory
        import my_custom_module
      
  2. Modifying PYTHONPATH Environment Variable

    • To make the change permanent, add the directory to the PYTHONPATH environment variable.

      • Linux/Mac: Add the following line to the shell configuration file (~/.bashrc, ~/.zshrc, etc.):

          export PYTHONPATH="/path/to/your/directory:$PYTHONPATH"
        
      • Windows: Update the PYTHONPATH variable in the system environment variables.

User Defined Modules

  • A user-defined module is a Python file we create to organize reusable code, such as functions, classes, or variables, for use across multiple programs.

Creating a user-defined module

  1. Create a Python file (e.g., mymodule.py):

     # mymodule.py
     def greet(name):
         return f"Hello, {name}!"
    
     pi_value = 3.14159
    
  2. Import and use the module:

     import mymodule
    
     print(mymodule.greet("Alice"))  # Output: Hello, Alice!
     print(mymodule.pi_value)       # Output: 3.14159
    
  • When we create a module, Python initializes some extra values, one of them is a variable called __name__. The value of this variable is dependent on how the file is run.

  • We can add a __name__ == "__main__" block to your module for testing its functionality. When imported, the code inside this block won't execute.

      # my_module.py
      def greet(name):
          return f"Hello, {name}!"
    
      if __name__ == "__main__":
          print(greet("Test User"))  # Output: Hello, Test User!
    
  • To create a private value in Python, we can follow a convention by preceding the variable name with one or two underscores.


Packages

  • In Python, a package is a way of organizing related Python modules into a directory hierarchy.

  • A package allows to structure the codebase logically, making it easier to manage, reuse, and scale your code.

  • Packages provide namespaces, which help avoid naming conflicts between modules.

What is a Package?

  • A package is essentially a directory containing a special file named __init__.py.

  • This file can be empty or contain initialization code for the package.

  • The presence of __init__.py (not mandatory in Python 3.3 and later) indicates to Python that the directory should be treated as a package.

Creating a Package

  • Create a package named my_package with two modules: module1.py and module2.py.

      my_package/
          __init__.py
          module1.py
          module2.py
    

PIP

  • pip is the package installer for Python.

  • It allows to install, manage, and uninstall Python packages from the Python Package Index (PyPI) and other package repositories.

  • It is the default package manager for Python and is widely used in Python development.

Common pip Commands

Installing a Package

pip install package_name

Installing a Specific Version

pip install package_name==version

Upgrading a Package

pip install --upgrade package_name

Uninstalling a Package

pip uninstall package_name

Listing Installed Packages (locally)

pip list

Checking Package Information

pip show package_name

Generating a Requirements File

pip freeze > requirements.txt
  • This creates a requirements.txt file listing all installed packages and their versions, which is useful for sharing or replicating environments.

Installing Packages from a Requirements File

pip install -r requirements.txt

Searching for a Package

pip search package_name
  • (Note: This command is deprecated in recent versions of pip.)

Checking for Outdated Packages

pip list --outdated