Python Modules, Libraries, and Packages


Python Scripts

A Python script is a file containing code written in the Python programming language.

  • automates tasks that would otherwise be executed line by line in Python’s interactive mode
  • has .py extension
  • can be run from command line

Python Modules

A Python module is a file containing Python definitions and statements.

  • provides a way to logically organize Python code
  • includes:
    • functions
    • classes
    • variables
  • imported into other modules or scripts with import
  • common Python modules for network automation:
    • Netmiko for SSH connections to routers and switches
    • NAPALM provides a set of functions to interact with different netowork device OSs
    • Ansible is an open-source software provisioning, configuration management, and application deployment tool
    • Paramiko is used for implementing SSHv2 protocol
    • Exscript automates Telnet and SSH connections to remote hosts

Python Packages

A Python package is a collection of Python modules, or files containing Python code, organized in a directory hierarchy.

  • allows for a structured, modular approach to programming
  • installed using a package manager such as pip

Python Libraries

A Python library is a reusable chunk of code that you may want to include in your programs/projects.

  • provides pre-written functionality
  • in context of network automation, libraries offer pre-built modules for common networking tasks
  • common network automation libraries:
    • Requests for making HTTP requests
    • BeautifulSoup for parsing HTML and XML documents
    • Scapy for crafting, sending, and receiving packets

Packages vs Libraries

  • both are used to enhance functionality of Python
  • A library is a collection of modules
  • A package is a way of organizing related modules or libraries into a single directory hierarchy
  • both can be imported and used in a python script
  • key difference is in their structure
    • library is a single file containing reusable functions and classes
    • package is a directory that can contain multiple module files and a special __init__.py file to indicate that the directory is a package
      • allows for a hierarchical structuring of the code
      • makes packages ideal for large projects
  • all packages are libraries, but not all libraries are packages

Using Python Modules

Create and Use a Module

Creating a Python module involves writing a Python script with functions, classes, or variables, and saving it with a .py extension.

  • to use a module in a script, import it using the import statement
  • allows for code reuse across multiple scripts
    • e.g., import mymodule

Rename a Module

A module can be renamed when it’s imported using the as keyword.

  • allows for more intuitive and convenient usage
import network as net
 
ip = '192.168.1.1'
if net.ping_device(ip):
    print(f'Device {ip} is online.')
else:
    print(f'Device {ip} is offline.')

Built-in Modules

Built-in modules in Python are libraries that come pre-installed with Python.

  • provide functions and classes for a variety of tasks without the need for additional installation
  • e.g.,
    • os module provides functions for interacting with the OS
    • socket module is used for network communications

Example Using These Modules to get the Hostname and IP Address

import socket
import os
 
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
 
print(f'Hostname: {hostname}')
print(f'IP Address: {ip_address}')

List Function Names

Use the dir() function to list all function names in a Python module.

  • returns a list of names in the current local scope or a list of attributes of an object
  • when a module is passed as an argument,
    • returns a list of the module’s attributes, including its functions

Example

import math
functions = [name for name in dir(math) if callable(getattr(math, name))]
print(functions)
  • dir(math) returns a list of all attributes of the math module
  • the list comprehension filters this list to include only callable attributes (i.e., functions)
  • the result is a list of all function names
  • this method can be applied to any module to list its functions
  • this will also include built-in functions and classes
    • to exclude, need to use additional filtering criteria

Import from a Module

Specific parts of a module can be imported using the from ... import ... statement.

  • allows for importing only the necessary functions or classes
from network_tools import ping
 
ip = '192.168.1.1'
if ping(ip):
    print(f'Device {ip} is online.')
else:
    print(f'Device {ip} is offline.')
  • only the ping function is imported from the module

The ipaddress Module

The ipaddress module is a powerful tool for manipulating and analyzing IP addresses and networks.

  • provides classes for handling IPv4 and IPv6 addresses, networks, and interfaces
    • support validation, comparison, sorting, and other operations
  • automatically handles intricacies of IP addressing, such as excluding the network and broadcast addresses

Example

import ipaddress
 
# Define the network
network = ipaddress.ip_network('192.0.2.0/24')
 
# Iterate over all hosts in the network
for host in network.hosts():
    print(host)
  • ipaddress.ip_network('192.0.2.0/24') creates an IPv4 object
  • network.hosts() method is used to iterate over all hosts in the network
  • each host is printed to the console

The help Module

The help() function in Python is a built-in function that can be used to access the built-in documentation for Python modules, functions, classes, keywords, etc.

  • most commonly used in the interpreter
  • e.g., help(print)
    • displays documentation for print function
  • can be used on Python modules
import os
help(os)

Python Libraries

Install a Library

Python libraries can be installed using package managers like pip.

  • syntax: pip install library_name
  • recommended to use a virtual environment to avoid conflicts between libraries
  • upgrade a library: pip install --upgrade library_name
  • upgrade pip to latest version before install libraries

Standard Library

The Standard Library is a collection of modules and packages that come pre-installed with Python.

  • available in most Python installations
  • designed to enhance Python’s usability and standardize solutions for common programming tasks

Running a Library

  • first import it into a Python script
  • then call its functions or classes

Example

import socket
 
# Create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
# Define the server and the port
server = 'hostname'
port = 80
 
# Connect to the server
s.connect((server, port))
  • socket library is imported
  • socket function is used to create a socket object
  • connect method is used to establish a connection to the server

Python Packages

Listing a Package

Use pip list in CLI to return a list of all installed packages and their versions.

  • to check for a specific package: pip show package_name

Installing a Package

Install packages using pip with the pip install package_name command in the CLI.

  • recommended to use a virtual environment to avoid conflicts between packages
  • upgrade packages with pip install --upgrade package_name
  • ensure pip is upgraded before installing packages

Using a Package

  • import the package into a Python script
  • access functions and classes using dot notation

Example

from netmiko import ConnectHandler
 
device = {
    'device_type': 'cisco_ios',
    'ip':   '10.0.0.1',
    'username': 'admin',
    'password': 'password',
}
 
connection = ConnectHandler(**device)
 
output = connection.send_command('show ip int brief')
print(output)
  • the ConnectHandler class from the netmiko package is used to establish an SSH connection to a device
  • send_command method is then used to send a command to the device and print the output

Removing a Package

Remove/uninstall a package with pip uninstall package_name in the CLI.

  • -y option to skip the confirmation
    • e.g., pip uninstall -y package_name