Chapter 4: Modules and packages#

In Python, modules and packages are used to organize code into reusable units. A module is simply a file containing Python definitions and statements, while a package is a collection of modules placed in a directory hierarchy.

Modules allow you to organize code logically and reuse it in other programs. For example, you could write a module that contains functions for working with strings, and then use that module in multiple programs. To use a module in a program, you simply import it using the import statement.

Packages, on the other hand, are collections of related modules that are organized in a directory hierarchy. A package can contain sub-packages, which are simply packages within packages. This allows you to organize your code into logical units that can be easily reused and shared.

For example, you could have a package called math that contains sub-packages for working with different types of math functions, such as geometry and algebra. Each sub-package would contain its own modules, such as circles and triangles in the geometry package.

Overall, modules and packages help make Python code more organized, reusable, and maintainable.

How to use Modules#

To use a module or package in your Python code, you can use the import statement or the from-import statement.

The import statement is used to import an entire module or package. For example, to import the math module, you would use the following statement:

The math module#

import math

This statement makes all the functions and variables defined in the math module available in your code. To use a function from the math module, you would call it using the dot notation, like this:

result = math.sqrt(16)

The from-import statement is used to import specific functions or variables from a module or package. For example, to import only the sqrt function from the math module, you would use the following statement:

from math import sqrt

This statement makes only the sqrt function available in your code. To use it, you can simply call it by name, like this:

result = sqrt(16)

You can also use the from-import statement to import multiple functions or variables from a module or package, like this:

from math import sqrt, pow

This statement makes both the sqrt and pow functions available in your code.

Note that you can also give a module or function an alias using the as keyword, like this:

import math as m
from math import sqrt as s

This can be useful if you want to use a shorter or more descriptive name for a module or function.

Python comes with a large standard library of modules that provide useful functionality for a wide range of tasks. Here are some examples of how to use a few of the standard library modules:

The math module provides functions for mathematical operations. Here’s an example of how to use the sqrt function to calculate the square root of a number:

import math

result = math.sqrt(16)
print(result) # Output: 4.0

The random module#

The random module provides functions for generating random numbers. Here’s an example of how to use the randint function to generate a random integer between 1 and 10:

import random

result = random.randint(1, 10)
print(result) # Output: a random integer between 1 and 10

The datetime module#

The datetime module provides functions for working with dates and times. Here’s an example of how to use the datetime function to create a datetime object representing the current date and time:

import datetime

now = datetime.datetime.now()
print(now) # Output: the current date and time
2024-10-02 01:18:40.061585

The os module#

The os module provides functions for interacting with the operating system. Here’s an example of how to use the getcwd function to get the current working directory:

import os

cwd = os.getcwd()
print(cwd) # Output: the current working directory

Overall, the standard library modules provide a wide range of functionality that can save you time and effort when writing Python code.

To create your own module in Python, you simply need to create a new Python file with a .py extension and define some functions or variables in it. For example, you could create a file called my_module.py with the following content:

def greet(name):
 print(f"Hello, {name}!")

def goodbye(name):
 print(f"Goodbye, {name}!")

Once you have created your module file, you can import it into your Python code using the import statement. For example, if you have saved the my_module.py file in the same directory as your main Python script, you can import it like this:

import my_module

my_module.greet("Alice") # Output: Hello, Alice!
my_module.goodbye("Bob") # Output: Goodbye, Bob!

If you want to create a package of multiple modules, you need to create a directory with an __init__.py file in it. The __init__.py file tells Python that the directory is a package and can contain modules. Here’s an example of how to create a package called my_package:

  1. Create a directory called my_package.

  2. Create a file called __init__.py in the my_package directory. This file can be empty or can contain some initialization code for the package.

  3. Create one or more module files in the my_package directory. For example, you could create a file called my_module.py with the same content as in the previous example.

  4. To use the greet function from the my_module module in your code, you would import it like this:

from my_package.my_module import greet

greet("Alice") # Output: Hello, Alice!

Note that the __init__.py file can also contain code to import other modules or sub-packages in the package, as well as initialization code for the package as a whole. This allows you to create complex packages with multiple layers of modules and sub-packages.

To install packages in Python, you can use the pip command-line tool. pip is a package installer for Python that makes it easy to download and install third-party packages from the Python Package Index (PyPI). Here are the steps to install a package using pip:

  1. Open a command prompt or terminal window.

  2. Type python -m pip install <package_name> and press Enter. Replace <package_name> with the name of the package you want to install. For example, to install the numpy package, you would type python -m pip install numpy.

  3. Wait for pip to download and install the package and any dependencies.

Here’s an example of how to install the requests package, which is a popular package for making HTTP requests in Python:

python -m pip install requests

Once you have installed a package, you can import it into your Python code just like any other module. For example, if you have installed the requests package, you can use it like this:

import requests

response = requests.get("https://www.google.com")
print(response.status_code)
200

This example uses the requests package to send an HTTP GET request to the Google homepage and print the response status code (which should be 200 if the request was successful).

When you install a Python package using pip, it will automatically download and install any dependencies that the package requires. If a dependency itself has additional dependencies, pip will also download and install those dependencies, and so on, until all dependencies are installed.

For example, if you install the pandas package, which is a popular data analysis library for Python, pip will also download and install the numpy package, which is a dependency of pandas. Similarly, if you install the numpy package, pip will download and install the mkl-service package, which is a dependency of numpy.

Here’s an example of how to install the pandas package using pip:

python -m pip install pandas

When you run this command, pip will download and install the pandas package and its dependencies, including numpy.

If you want to install a specific version of a package or a package with specific dependencies, you can specify these requirements using the == operator and the name of the package and version number you want to install. For example, to install version 1.0.0 of the pandas package, you would run:

python -m pip install pandas==1.0.0

Or to install a package with specific dependencies, you can use the requirements.txt file to list the dependencies along with their version numbers, and then install the package using pip with the -r flag to specify the requirements file. For example:

  1. Create a file called requirements.txt with the following content:

numpy==1.18.5
pandas==1.0.0
  1. Install the package using pip with the -r flag:

python -m pip install -r requirements.txt

This will install the numpy and pandas packages with the specified version numbers. If any of these packages have additional dependencies, pip will download and install those as well.

Python’s standard library provides several modules for parsing XML documents. One of the most commonly used modules is xml.etree.ElementTree, which provides a simple and efficient API for parsing and manipulating XML documents.

Here’s an example of how to use xml.etree.ElementTree to parse an XML document:

import xml.etree.ElementTree as ET

# parse the XML file
tree = ET.parse('example.xml')

# get the root element of the document
root = tree.getroot()

# iterate over the child elements of the root element
for child in root:
 # do something with the child element
 print(child.tag, child.attrib)
child1 {'foo': 'bar'}
child2 {'baz': 'qux'}

In this example, we first import the xml.etree.ElementTree module and then use its parse() function to parse an XML file called example.xml. We then get the root element of the document using the getroot() method and iterate over its child elements using a for loop.

For each child element, we print its tag name and attribute values using the tag and attrib properties, respectively.

Here’s an example of what the XML file might look like:

<root>
 <child1 foo="bar">
 <grandchild1/>
 <grandchild2/>
 </child1>
 <child2 baz="qux">
 <grandchild3/>
 </child2>
</root>

When we run the Python code above with this XML file, it will output:

child1 {'foo': 'bar'}
child2 {'baz': 'qux'}

This is just a simple example of how to parse an XML document using Python’s standard library. The xml.etree.ElementTree module provides many other features for parsing and manipulating XML documents, including support for namespaces, XPath expressions, and more. You can find more information about these features in the Python documentation.