PyCUPS - Python CUPS Bindings
PyCUPS provides Python bindings for libcups, the Common Unix Printing System library. It allows Python applications to interact with CUPS servers to manage printers, print jobs, and monitor printer status. The current version is 2.0.4, with releases primarily focusing on bug fixes and compatibility with newer Python versions, rather than new features.
Common errors
-
ModuleNotFoundError: No module named 'cups'
cause The 'cups' module could not be found, either because pycups was not installed or its C extensions failed to compile.fixFirst, ensure pycups is installed: `pip install pycups`. If the error persists, it's likely due to missing system-level CUPS development headers. Install them (e.g., `sudo apt-get install libcups2-dev` on Debian/Ubuntu) and then reinstall pycups. -
TypeError: an integer is required (got type bytes) in method 'IPPRequest_writeIO'
cause This specific error often occurred with Python 3.10 due to changes in C API handling of `Py_ssize_t` causing type mismatches during IPP request/response processing.fixUpgrade pycups to version 2.0.3 or newer, which includes a fix by defining `PY_SSIZE_T_CLEAN` to ensure correct type handling for Python 3.10+. -
cups.IPPError: 1007
cause This is a generic CUPS IPP error indicating 'bad request' or similar. Common reasons include the CUPS server not being reachable, a printer not existing, incorrect printer URI, or authentication failure.fixCheck the CUPS server status (`sudo systemctl status cups`). Verify the printer name/URI is correct. Ensure the user running the Python script has appropriate permissions to access the CUPS server and printers.
Warnings
- gotcha PyCUPS requires system-level CUPS development headers (e.g., `libcups2-dev` on Debian/Ubuntu, `cups-devel` on Fedora/RHEL) to be installed on the system *before* `pip install pycups`. Without these, the installation will fail with compilation errors.
- gotcha Many functions in PyCUPS interact directly with the CUPS server. If the CUPS service is not running or accessible, operations like `cups.Connection()` or `conn.getPrinters()` will raise `cups.IPPError`.
- gotcha When handling IPP attributes (like printer names, job titles) with non-ASCII characters, be mindful of encoding. While PyCUPS aims to handle UTF-8, explicit encoding/decoding may be necessary for certain operations or when integrating with other systems.
Install
-
pip install pycups
Imports
- cups
import cups
Quickstart
import cups
# Connect to the local CUPS server
conn = cups.Connection()
# List all available printers
printers = conn.getPrinters()
if printers:
print("Available Printers:")
for printer_name, printer_attrs in printers.items():
print(f" - {printer_name}")
# print(f" Location: {printer_attrs.get('printer-location')}")
# print(f" State: {printer_attrs.get('printer-state-message')}")
else:
print("No printers found or CUPS server is not accessible.")
# Example: Get default printer (if set)
default_printer = conn.getDefault() # Returns string or None
if default_printer:
print(f"\nDefault Printer: {default_printer}")