Developer Documentation
Python
Updated September 19, 2026

Generate QR Codes in Python

Generate QR codes for text or URLs and export them as images in Python.

QR code libraries handle matrix construction, masking, and error correction for you. Your application still needs to choose suitable error correction, margin, image size, and payload encoding.

Quick Start#

The following example uses the standard Python API or a widely used package for this task.

Using qrcode#

Python
1# pip install qrcode[pil]2import qrcode34img = qrcode.make("https://toolporta.com")5img.save("toolporta.png")

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Control correction and margin#

Python
1import qrcode2from qrcode.constants import ERROR_CORRECT_M34qr = qrcode.QRCode(5    error_correction=ERROR_CORRECT_M,6    box_size=,7    border=,8)9qr.add_data("https://toolporta.com")10qr.make(fit=True)11qr.make_image().save("toolporta.png")

Important Notes#

Keep the default four-module quiet zone unless you have tested scanning reliability in the intended environment.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement QR Code in other languages: