Useful extra bits for Python
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
- breaking The 'extras' library has not been updated since May 2016. It may not be compatible with modern Python versions (e.g., Python 3.8+) or contain practices that are now considered outdated or superseded by standard library features. Use with caution for new projects.
- gotcha The name 'extras' can be confusing due to its overlap with the concept of 'extras' (optional dependencies) in Python packaging (e.g., `pip install package[extra_feature]`). This library 'extras' is a collection of utilities, distinct from packaging 'extras'. Also, there are other similarly named PyPI packages like 'python-extras' or 'streamlit-extras' which are different projects.
Install
-
pip install extras
Imports
- try_import
from extras import try_import
- try_imports
from extras import try_imports
- safe_hasattr
from extras import safe_hasattr
Quickstart
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.")