Registry / serialization / python-datauri

python-datauri

JSON →
library3.0.2pypypi✓ verified 87d ago

A small Python library for manipulating data URIs, as defined in RFC2397. It provides functionalities for parsing existing data URIs into structured objects and creating new ones from various inputs. The library is currently at version 3.0.2 and appears to have an infrequent release cadence, driven by feature additions and Python version compatibility.

pip install python-datauri
INSTALL
IMPORT
SIG · PYTHON-DATAURI
P
python-datauri
serializationpythonv3.0.2
Install
1.6s avg
Import
212ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.0.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.226s · 18.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.197s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DataURI
from datauri import DataURI
The primary class for creating and parsing data URIs.

This quickstart demonstrates how to parse an existing data URI and access its components, and how to create a new data URI from specified media type and data. It highlights the use of `DataURI.data` for bytes and `DataURI.text` for string representations.

from datauri import DataURI # Parsing a data URI uri_string = 'data:text/plain;charset=utf-8;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu' parsed_uri = DataURI(uri_string) print(f"MIME Type: {parsed_uri.mimetype}") print(f"Charset: {parsed_uri.charset}") print(f"Is Base64: {parsed_uri.is_base64}") print(f"Decoded data (bytes): {parsed_uri.data}") print(f"Decoded data (text): {parsed_uri.text}") # Creating a data URI made_uri = DataURI.make('image/png', base64=True, data=b'\x89PNG\r\n...') # Example with bytes print(f"Created URI: {made_uri}") # Creating from a string directly text_uri = DataURI.make('text/plain', data='Hello, World!') print(f"Text URI: {text_uri}")
Debug
Known issues
breakingThe library underwent a major version bump from 2.x to 3.x (v3.0.0 released Jan 3, 2025). While explicit breaking changes are not extensively documented in the README for this specific jump, major version changes typically introduce backwards-incompatible changes. Users upgrading from 2.x should review their code for compatibility.
fix
Refer to the GitHub release notes or compare API usage between versions if encountering issues after upgrading.
affects: >=3.0.0
gotchaThe `DataURI.data` attribute always returns the decoded data as `bytes`. If you need a string representation (assuming the data is textual and decodable), you must use the `DataURI.text` attribute.
fix
Use `parsed_uri.text` when expecting string data, and `parsed_uri.data` for raw byte data.
affects: All
gotchaThe library's GitHub README notes that it is 'not very robust, and will reject a number of valid data URIs.' It primarily supports cases with a mimetype, charset, and base64 flag. Complex or non-standard data URIs might not parse correctly.
fix
Be aware of this limitation when working with diverse or malformed data URIs. For highly robust data URI parsing across all RFC specifications, consider `urllib.request.urlopen` in Python's standard library for basic decoding, or other more comprehensive solutions if available.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'python_datauri'
The Python package is installed via `pip install datauri`, but users sometimes mistakenly try to import a module named `python_datauri` based on the project's PyPI name or common naming conventions.
fix
Use `import datauri` instead, as the installed module is named 'datauri'.
ValueError: Invalid Data URI
The string provided to the `datauri.DataURI` constructor does not conform to the RFC2397 specification for data URIs, often missing the 'data:' prefix or having an incorrect format.
fix
Ensure the input string is a valid data URI, starting with 'data:' and following the correct structure. Example: `datauri.DataURI('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==')`
TypeError: data must be bytes or string, got <class 'int'>
When constructing a new `DataURI` object (e.g., `datauri.DataURI(data=...)`), the `data` parameter received an unsupported type (like an integer, list, or dict) instead of a `bytes` object or a `str`.
fix
Provide the `data` parameter as a `bytes` object for binary data or a `str` object for text data. Example: `datauri.DataURI(mimetype='text/plain', data='Hello, World!')` or `datauri.DataURI(mimetype='application/octet-stream', base64=True, data=b'\x01\x02\x03')`
Upgrade
Version history
3.0.2latest on PyPI · released Jan 3, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources