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
Create the environment
mkdir wifi-qr cd wifi-qr python -m venv .venvA
.venvfolder 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.Install the package
.venv\Scripts\python -m pip install "qrcode[pil]" # Successfully installed colorama-0.4.6 pillow-13.0.0 qrcode-8.2On Linux and macOS the path differs:
.venv/bin/python.[pil]installs Pillow along with it; without it you can’t save a PNG.Write the script
The file
wifi_qr.pyin 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")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.pngPoint a phone camera at the code in the terminal or at
wifi-guest.png— the phone offers to joinGuest.
What’s new here
- Packages:
pip. Anything not in the standard library is installed from the PyPI index withpip. 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 barepipmay belong to a different Python. - A virtual environment.
python -m venv .venvcreates 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
qrcodewas 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
.ps1script. - Fix
- Activation isn’t required: call the environment’s
pythonby 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
Guestandguestare 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.