basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    A QR code for guest Wi-Fi: your first pip package in a virtual environment

    Make an image a guest scans with a phone camera to join the Wi-Fi without typing the password. The code is drawn by the qrcode package — it isn’t in the standard library, so for the first time you install a package: into the folder’s virtual environment, so it doesn’t mix with the system Python.

    New herePackages: pipVirtual environments

    What you end up with

    wifi-qr\
      .venv\               the environment: its own python and the qrcode, pillow packages
      wifi_qr.py           the script
      wifi-guest.png       the image, to print or show on a screen

    The script prints the string for the QR code, draws the code right in the terminal, and saves a PNG.

    Step by step

    1. Create the environment

      mkdir wifi-qr
      cd wifi-qr
      python -m venv .venv

      A .venv folder appears — a copy of the interpreter with its own list of packages. Packages installed here aren’t visible to the system Python or other projects.

    2. Install the package

      .venv\Scripts\python -m pip install "qrcode[pil]"
      # Successfully installed colorama-0.4.6 pillow-13.0.0 qrcode-8.2

      On Linux and macOS the path differs: .venv/bin/python. [pil] installs Pillow along with it; without it you can’t save a PNG.

    3. Write the script

      The file wifi_qr.py in the same folder; the network name and password are yours:

      import qrcode
      
      SSID = "Guest"
      PASSWORD = "tea-and-cookies-42"
      SECURITY = "WPA"  # WPA covers WPA2 and WPA3 too; for an open network use "nopass"
      
      
      def escape(text):
          """The characters \\ ; , : " are escaped with a backslash in the WIFI: format."""
          for char in '\\;,:"':
              text = text.replace(char, "\\" + char)
          return text
      
      
      data = f"WIFI:T:{SECURITY};S:{escape(SSID)};P:{escape(PASSWORD)};;"
      print(data)
      
      qr = qrcode.QRCode(border=2)
      qr.add_data(data)
      qr.print_ascii(invert=True)
      
      image = qr.make_image()
      image.save("wifi-guest.png")
      print("Saved: wifi-guest.png")
    4. Run it

      .venv\Scripts\python wifi_qr.py
      # WIFI:T:WPA;S:Guest;P:tea-and-cookies-42;;
      # …a QR code drawn in characters…
      # Saved: wifi-guest.png

      Point a phone camera at the code in the terminal or at wifi-guest.png — the phone offers to join Guest.

    What’s new here

    • Packages: pip. Anything not in the standard library is installed from the PyPI index with pip. Run it as a module of the interpreter you want — python -m pip — and the package is sure to land where it will be imported from. A bare pip may belong to a different Python.
    • A virtual environment. python -m venv .venv creates a folder with a separate Python for the project. One project’s packages don’t get in another’s way, and what you don’t need goes away with the folder. An environment isn’t moved: on another computer you create it anew.

    The WIFI string format

    WIFI:T:WPA;S:Guest;P:password;; — the security type, the network name, the password, and two semicolons at the end. If the name or password contains \, ;, ,, : or ", they must be escaped — the escape function does that. For a hidden network, add H:true; before ;;.

    The password sits in the script in plain text, so the QR code is for a guest network kept apart from home devices (Guest network on MikroTik).

    If it doesn’t work

    ModuleNotFoundError: No module named 'qrcode'

    Cause
    The script was run with the system python, while the package is in .venv: each environment has its own packages.
    Fix
    Run it through the environment: .venv\Scripts\python wifi_qr.py (Linux, macOS — .venv/bin/python).

    ModuleNotFoundError: No module named 'PIL' on the make_image line

    Cause
    qrcode was installed without Pillow: it prints the code in the terminal but draws PNGs through Pillow.
    Fix
    .venv\Scripts\python -m pip install "qrcode[pil]" — the square brackets hold an optional part of the package.

    Activate.ps1 cannot be loaded because running scripts is disabled on this system

    Cause
    PowerShell doesn’t run scripts by default, and activating the environment is a .ps1 script.
    Fix
    Activation isn’t required: call the environment’s python by its path, as in this page. If you need it: Set-ExecutionPolicy -Scope CurrentUser RemoteSigned.

    error: externally-managed-environment on pip install on Ubuntu

    Cause
    Ubuntu 23.04 and newer won’t let you install packages into the system Python (PEP 668).
    Fix
    Install into an environment, as here: python3 -m venv .venv, then .venv/bin/python -m pip install "qrcode[pil]".

    The camera sees the code but doesn’t join the network

    Cause
    A typo in the network name or the password: case matters, and Guest and guest are different networks.
    Fix
    Compare the WIFI:… line the script prints first with the router’s settings.

    How to check

    A phone that doesn’t know the network yet scans wifi-guest.png with its camera and joins without typing the password.