Useful extra bits for Python

1.0.0 · abandoned · verified Sun Apr 12

The 'extras' library provides small, useful extensions to the Python standard library. It was originally developed to make code within the 'testtools' project cleaner, offering utilities like robust module import attempts and safe attribute checking. The last release was in May 2016, indicating it is no longer actively maintained.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the `try_import` function, a core utility from the 'extras' library. It attempts to import the 'requests' library and then uses it if successful, gracefully handling cases where 'requests' might not be installed. This pattern is useful for optional dependencies.

from extras import try_import

# Attempt to import a module, returning None if it fails
requests = try_import("requests")

if requests:
    print("The 'requests' library was imported successfully.")
    try:
        # Use an external service for a live example, if requests is available
        response = requests.get("https://api.github.com")
        print(f"GitHub API status: {response.status_code}")
    except Exception as e:
        print(f"Error accessing GitHub API: {e}")
else:
    print("The 'requests' library is not installed. Skipping API call.")

view raw JSON →