Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
Change log
==========

WIP
===

- Allow execution as a Python module (@stefansjs in `#400`_)::

python -m qrcode --output qrcode.png "hello world"

.. _#400: https://github.com/lincolnloop/python-qrcode/pull/400

8.2 (01 May 2025)
=================

Expand Down
3 changes: 3 additions & 0 deletions qrcode/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .console_scripts import main

main()
62 changes: 62 additions & 0 deletions qrcode/tests/test_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import subprocess
import sys
import tempfile
from pathlib import Path

import pytest

try:
from PIL import Image, ImageDraw # noqa: F401

PIL_NOT_INSTALLED = False
except ImportError:
PIL_NOT_INSTALLED = True


def test_module_help():
"""Test that the module can be executed with the help flag."""
result = subprocess.run(
[sys.executable, "-m", "qrcode", "-h"],
capture_output=True,
text=True,
check=False,
)

# Check that the command executed successfully
assert result.returncode == 0

# Check that the help output contains expected information
assert "qr - Convert stdin" in result.stdout
assert "--output" in result.stdout
assert "--factory" in result.stdout


@pytest.mark.skipif(PIL_NOT_INSTALLED, reason="PIL is not installed")
def test_module_generate_qrcode():
"""Test that the module can generate a QR code image."""
with tempfile.TemporaryDirectory() as temp_dir:
output_path = Path(temp_dir) / "qrcode.png"

# Run the command to generate a QR code
result = subprocess.run(
[
sys.executable,
"-m",
"qrcode",
"--output",
str(output_path),
"https://github.com/lincolnloop/python-qrcode",
],
capture_output=True,
text=True,
check=False,
)

# Check that the command executed successfully
assert result.returncode == 0

# Check that the output file was created
assert output_path.exists()

# Check that the file is not empty and is a valid image file
assert output_path.stat().st_size > 0