PyJokes
PyJokes is a Python library that provides one-liner jokes, primarily focusing on programmer humor. It also supports general and neutral joke categories, and multiple languages. Currently at version 0.8.3, it maintains a stable API with infrequent but consistent updates.
Common errors
-
ModuleNotFoundError: No module named 'pyjokes'
cause The `pyjokes` library has not been installed in the current Python environment.fixInstall the package using pip: `pip install pyjokes` -
KeyError: 'chuck'
cause An unsupported joke category was requested. The 'chuck' category was removed in recent versions, but may still appear in older documentation or examples.fixUse one of the currently supported categories: 'programmer', 'neutral', or 'all'. You can check `pyjokes.CATEGORIES` for a definitive list. -
AttributeError: 'list' object has no attribute 'strip'
cause You called `pyjokes.get_jokes()` which returns a list of jokes, but then attempted to apply a string method (like `.strip()`) directly to this list instead of its individual elements.fixIterate over the list to process each joke string, or use `pyjokes.get_joke()` if you only need a single joke. Example: `for joke in pyjokes.get_jokes(): print(joke.strip())`
Warnings
- breaking The `chuck` joke category, previously advertised or available in older versions/README, is no longer supported as of version 0.8.0+. Attempting to request jokes from this category will lead to a `KeyError` or unexpected behavior, as it has been removed from the core joke data.
- gotcha Using `pyjokes.get_joke()` with a non-existent or unsupported language/category combination might silently fall back to default English/all jokes instead of raising an error. This can lead to unexpected joke content if you expect specific localization or theme.
- gotcha The functions `pyjokes.get_joke()` and `pyjokes.get_jokes()` return different types: `get_joke()` returns a single string, while `get_jokes()` returns a list of strings. Confusing these can lead to `TypeError` when attempting string operations on a list or iterating over a string.
Install
-
pip install pyjokes
Imports
- get_joke
from pyjokes import get_joke
- get_jokes
from pyjokes import get_jokes
Quickstart
import pyjokes
# Get a random programmer joke
programmer_joke = pyjokes.get_joke(category='programmer')
print("Programmer Joke:", programmer_joke)
# Get a random neutral joke in German
try:
german_joke = pyjokes.get_joke(language='de', category='neutral')
print("German Neutral Joke:", german_joke)
except Exception as e:
print(f"Could not fetch German joke (might not be available): {e}")
# Get a list of all jokes (careful, can be long!)
# all_jokes_list = pyjokes.get_jokes(category='all', language='en')
# print(f"First 3 jokes from list: {all_jokes_list[:3]}")