{"id":3778,"library":"python-barcode","title":"python-barcode","description":"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.","status":"active","version":"0.16.1","language":"en","source_language":"en","source_url":"https://github.com/WhyNotHugo/python-barcode","tags":["barcode","generator","svg","png","ean","upc","code128","imaging"],"install":[{"cmd":"pip install python-barcode","lang":"bash","label":"Base installation (SVG output)"},{"cmd":"pip install \"python-barcode[images]\"","lang":"bash","label":"Installation with Pillow for image output (e.g., PNG, JPEG)"}],"dependencies":[{"reason":"Required for generating barcode images (e.g., PNG, JPEG, GIF); not needed for SVG output.","package":"Pillow","optional":true}],"imports":[{"note":"Common barcode type, directly importable from the top-level package.","symbol":"EAN13","correct":"from barcode import EAN13"},{"note":"The writer class for generating SVG format barcodes.","symbol":"SVGWriter","correct":"from barcode.writer import SVGWriter"},{"note":"The writer class for generating image formats (requires Pillow).","symbol":"ImageWriter","correct":"from barcode.writer import ImageWriter"},{"note":"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`.","wrong":"from barcode import Code128","symbol":"Code128","correct":"from barcode.codex import Code128"}],"quickstart":{"code":"import os\nfrom io import BytesIO\nfrom barcode import EAN13, Code128\nfrom barcode.writer import SVGWriter, ImageWriter\n\n# Example 1: Generate EAN13 as SVG\nnumber_ean13 = '590123412345' # 12 digits, 13th (checksum) is calculated\nean = EAN13(number_ean13, writer=SVGWriter())\nwith open('ean13_barcode.svg', 'wb') as f:\n    ean.write(f)\nprint('Generated ean13_barcode.svg')\n\n# Example 2: Generate Code128 as PNG (requires Pillow)\ntry:\n    # Use a dummy number for Code128, it supports alphanumeric data\n    code128_data = 'MY-PRODUCT-ABC-123'\n    code = Code128(code128_data, writer=ImageWriter())\n    # To write to a file directly, ensure the output path is writable\n    # code.write(open('code128_barcode.png', 'wb'))\n\n    # Or, to an in-memory byte stream, then save\n    # This pattern is good for web apps or if you don't want to save to disk immediately\n    fp = BytesIO()\n    code.write(fp)\n\n    # Simulate saving from BytesIO if Pillow is available\n    from PIL import Image\n    fp.seek(0) # Rewind to the beginning of the stream\n    img = Image.open(fp)\n    img.save('code128_barcode.png')\n    print('Generated code128_barcode.png (requires Pillow)')\n\nexcept ImportError:\n    print(\"Pillow not installed. Skipping PNG generation. Install with: pip install \\\"python-barcode[images]\\\".\")\nexcept Exception as e:\n    print(f\"Error generating Code128 PNG: {e}\")\n","lang":"python","description":"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."},"warnings":[{"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.","message":"Older 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.","severity":"breaking","affected_versions":"<=0.15.0"},{"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.","message":"Barcode 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.","severity":"gotcha","affected_versions":"All"},{"fix":"Install `Pillow` using the extra: `pip install \"python-barcode[images]\"`. Ensure Pillow is available in your environment when using `ImageWriter`.","message":"Generating 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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":"All (when used with PyInstaller)"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}