Registry / serialization / python-barcode

python-barcode

JSON →
library0.16.1pypypi✓ verified 25d ago

python-barcode provides a simple way to create standard barcodes in Python. It requires no external dependencies for generating SVG files. For outputting image formats like PNGs, the optional Pillow library is needed. The current version is 0.16.1, supporting Python 3.9 up to 3.13. The library maintains an active development status with regular updates addressing Python compatibility and bug fixes.

pip install python-barcode
INSTALL
IMPORT
SIG · PYTHON-BARCODE
P
python-barcode
serializationpythonv0.16.1
Install
1.9s avg
Import
122ms
Disk
36MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.16.1 · 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.910 runs
installs and imports cleanly · install 0.0s · import 0.130s · 38MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.9s · import 0.113s · 39MB
36MB installed
● package 36MB
Code
Verified usage

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

EAN13
from barcode import EAN13
Common barcode type, directly importable from the top-level package.
SVGWriter
from barcode.writer import SVGWriter
The writer class for generating SVG format barcodes.
ImageWriter
from barcode.writer import ImageWriter
The writer class for generating image formats (requires Pillow).
Code128
from barcode.codex import Code128
from barcode import Code128
In older versions, some barcode types like Code128 might have been directly importable from `barcode`. Since version 0.10.0 and above, most codex-based barcode types (like Code128, Code39) are located under `barcode.codex`.

This quickstart demonstrates generating an EAN13 barcode as an SVG file and a Code128 barcode as a PNG image. The PNG generation specifically highlights the dependency on Pillow and shows how to handle potential `ImportError`. It includes both direct file writing and in-memory byte stream handling for images.

import os from io import BytesIO from barcode import EAN13, Code128 from barcode.writer import SVGWriter, ImageWriter # Example 1: Generate EAN13 as SVG number_ean13 = '590123412345' # 12 digits, 13th (checksum) is calculated ean = EAN13(number_ean13, writer=SVGWriter()) with open('ean13_barcode.svg', 'wb') as f: ean.write(f) print('Generated ean13_barcode.svg') # Example 2: Generate Code128 as PNG (requires Pillow) try: # Use a dummy number for Code128, it supports alphanumeric data code128_data = 'MY-PRODUCT-ABC-123' code = Code128(code128_data, writer=ImageWriter()) # To write to a file directly, ensure the output path is writable # code.write(open('code128_barcode.png', 'wb')) # Or, to an in-memory byte stream, then save # This pattern is good for web apps or if you don't want to save to disk immediately fp = BytesIO() code.write(fp) # Simulate saving from BytesIO if Pillow is available from PIL import Image fp.seek(0) # Rewind to the beginning of the stream img = Image.open(fp) img.save('code128_barcode.png') print('Generated code128_barcode.png (requires Pillow)') except ImportError: print("Pillow not installed. Skipping PNG generation. Install with: pip install \"python-barcode[images]\".") except Exception as e: print(f"Error generating Code128 PNG: {e}")
python-barcode --version
Debug
Known issues
breakingOlder versions introduced significant API changes, such as moving writer classes from subpackages to modules (v0.5.0) and modifying the `render` method's API (v0.2.1). Version 0.14.0 changed default dimensions for better PNG/SVG consistency, and v0.15.x dropped support for Python 3.6. Always consult the changelog for major version upgrades.
fix
Review the official changelog when upgrading, especially for major and minor version bumps, to adapt to API changes or deprecated Python versions. For Python 3.6, upgrade your Python environment to 3.9 or newer.
affects: <=0.15.0
gotchaBarcode checksums are automatically calculated by the library for most standard formats (e.g., EAN13, ISBN13). Providing an input number that already includes a checksum, or one with an incorrect length, can lead to invalid barcodes or errors. For EAN13, provide a 12-digit number; the 13th check digit is added automatically.
fix
Always provide the base number without the check digit for barcode types where the library calculates it. Refer to the documentation for the specific length requirements of each barcode format.
affects: All
gotchaGenerating image formats (like PNG, JPEG) using `ImageWriter` requires the `Pillow` library to be installed. Without `Pillow`, attempts to use `ImageWriter` will result in an `ImportError` or other runtime errors related to missing image processing capabilities.
fix
Install `Pillow` using the extra: `pip install "python-barcode[images]"`. Ensure Pillow is available in your environment when using `ImageWriter`.
affects: All
gotchaWhen bundling applications with PyInstaller (or similar tools), `python-barcode` may fail to find its internal font files (`DejaVuSansMono.ttf`) for rendering text on barcodes, leading to `IOError` or similar exceptions.
fix
One common workaround is to modify the `writer.py` file within your `site-packages/barcode` directory (or a copied version for your project) to explicitly set `self.font_path = 'arial.ttf'` or embed `DejaVuSansMono.ttf` and adjust the path accordingly for your PyInstaller bundle. This often involves ensuring 'arial.ttf' is available in the system or bundled.
affects: All (when used with PyInstaller)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'barcode'
The python-barcode library is not installed in your current Python environment.
fix
pip install python-barcode
ImportError: You need to install Pillow to use ImageWriter
The Pillow library, an optional dependency required for generating barcode images in formats like PNG or JPEG, is not installed.
fix
pip install Pillow
barcode.errors.BarcodeNotFoundError: No barcode found for name 'XYZ'
The specified barcode type name (e.g., 'XYZ') is either misspelled or not a supported format by the library when using `barcode.get()`.
fix
Use a valid barcode type name (e.g., 'ean13', 'code128', 'itf') or import the specific barcode class directly (e.g., `from barcode import EAN13`).
Upgrade
Version history
0.16.1latest on PyPI · released Aug 27, 2025
Audit
Dependencies
PillowoptionalRequired for generating barcode images (e.g., PNG, JPEG, GIF); not needed for SVG output.
Agent activity
13 hits · last 30 days
node
12
Resources
python-barcode — pip install python-barcode · libregistry