Python Forum
problem in using pyqrcode module to create QRcode
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem in using pyqrcode module to create QRcode
#1
hi
I wanted to create a QR code by Python using pyqrcode module.(I copied this code from elsewhere)
I entered the below code in idle in Python 3.11.3
code:
import pyqrcode
qr=pyqrcode.create('hello')
qr.png('hello.png',scale=7)
after entering line 3, the below error was shown:
Error:
Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> qr.png('hello.png',scale=7) File "C:\Users\akbar\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyqrcode\__init__.py", line 462, in png builder._png(self.code, self.version, file, scale, File "C:\Users\akbar\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyqrcode\builder.py", line 1266, in _png import png ModuleNotFoundError: No module named 'png'
I do not know what is the problem. must I import a module with the name png. I checked dir(qr) and there is a png function in it.
any guidance is appreciated.
Yoriz write Aug-19-2023, 03:09 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
https://pypi.org/project/PyQRCode/ Wrote:Requirements
The pyqrcode module only requires Python 2.6, Python 2.7, or Python 3. You may want to install pypng in order to render PNG files, but it is optional. Note, pypng is a pure python PNG writer which does not require any other libraries.

Try installing https://pypi.org/project/pypng/
Reply
#3
hi Yoriz
I read your explanation but I do not know what I must do now.
can guide me on what I must do.
thanks
Reply
#4
Note that packages are installed from the shell (command line), not from within a python script.
if you installed the package as instructed by Yoriz, then just rerun the program

Notice that the error tracebacks, provide very specific detail on what's wrong with your script.
Reply
#5
I make QR codes for students like this:

import qrcode
import cv2
import csv
import os
import pyzbar.pyzbar as pyzbar

# function to make each QR code with name and student number as studentnumber_name_QR.png

def makeQRcode(savefile, key):
    # key is student number, numsnames[key] is name
    data = str(key) + ':' + numsnames[key]
    #data = makeStudentnrs()
    # instantiate QRCode object    
    qr = qrcode.QRCode(version=None, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=10, border=4)
    # add data to the QR code
    qr.add_data(data)
    # compile the data into a QR code array
    qr.make(fit=True)
    # transfer the array into an actual image
    img = qr.make_image(fill_color="black", back_color="white")
    # save it to a file
    img.save(savefile)


# make the qr codes from a dictionary with key = student number, value = name 

for key in numsnames.keys():
    filename = savepathQRcodes + f'{key}_{numsnames[key]}_QR.png'
    makeQRcode(filename, key)
I use pyzbar to read the QR codes, because I found I got errors using qrcode.

When you make the QR codes, it is a good idea to read them 1 time to see that you get what you should! Check the output against your input data.
Reply
#6
# Make environment
C:\code
λ python -m venv qr_env

C:\code
λ cd qr_env\

C:\code\qr_env
λ C:\code\qr_env\Scripts\activate

# Install
(qr_env) C:\code\qr_env
λ pip install PyQRCode pypng
Collecting PyQRCode
  Using cached PyQRCode-1.2.1-py3-none-any.whl
Collecting pypng
  Using cached pypng-0.20220715.0-py3-none-any.whl (58 kB)
Installing collected packages: PyQRCode, pypng
Successfully installed PyQRCode-1.2.1 pypng-0.20220715.0

(qr_env) C:\code\qr_env
λ ls
Include/  Lib/  Scripts/  pyvenv.cfg  qr_code.py

(qr_env) C:\code\qr_env
λ cat qr_code.py
import pyqrcode

qr=pyqrcode.create('hello')
qr.png('hello.png',scale=7)
(qr_env) C:\code\qr_env

# Run code
λ python qr_code.py
.... No errors

(qr_env) C:\code\qr_env
λ ls
Include/  Lib/  Scripts/  hello.png  pyvenv.cfg  qr_code.py
Reply
#7
hi snippsat
I am approximately a newbie in Python.
is this code in Windows or Linux and if I copy it in idle shell python3.11.3 in Windows, will it be run?
thanks
Reply
#8
It's for Windows(Python 3.11.3) i use cmder,commands are same for environment/install in for cmd.
So just open cmd and do this and it should work,don't have to make a virtual environment.
pip install PyQRCode pypng
Reply
#9
(Aug-22-2023, 09:01 PM)snippsat Wrote: It's for Windows(Python 3.11.3) i use cmder,commands are same for environment/install in for cmd.
So just open cmd and do this and it should work,don't have to make a virtual environment.
pip install PyQRCode pypng

hi
I downloaded and installed cmder.
When I wanted to act as you said step by step , the below problems were raised:
line 25 in your text:
Error:
(qr_env) λ ls 'ls' is not recognized as an internal or external command, operable program or batch file.
and line 29:
Error:
C:\code\qr_env (qr_env) λ cat qr_code.py 'cat' is not recognized as an internal or external command, operable program or batch file.
can you explain what are 1s or ls and cat ?
thanks
Reply
#10
(Aug-23-2023, 02:59 PM)akbarza Wrote: I downloaded and installed cmder.
cmder you most dowlload the full version.
Then unzip to C:\cmder then run Cmder.exe it will do install,then it should work
(Aug-23-2023, 02:59 PM)akbarza Wrote: can you explain what are 1s or ls and cat ?
ls list files, cat show content of files,these are common commands on Linux.
When it work these will also have --help.

C:\code
λ cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

  -A, --show-all           equivalent to -vET
  -b, --number-nonblank    number nonempty output lines, overrides -n
  -e                       equivalent to -vE
  -E, --show-ends          display $ at end of each line
  -n, --number             number all output lines
  -s, --squeeze-blank      suppress repeated empty output lines
  -t                       equivalent to -vT
  -T, --show-tabs          display TAB characters as ^I
  -u                       (ignored)
  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB
      --help     display this help and exit
      --version  output version information and exit

Examples:
  cat f - g  Output f's contents, then standard input, then g's contents.
  cat        Copy standard input to standard output.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
Full documentation <https://www.gnu.org/software/coreutils/cat>
or available locally via: info '(coreutils) cat invocation'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with memory_graph module akbarza 3 389 Mar-04-2024, 10:15 AM
Last Post: snippsat
  problem using coloeama module akbarza 3 586 Jan-08-2024, 07:31 AM
Last Post: akbarza
  problem in using subprocess module akbarza 5 1,045 Sep-24-2023, 02:02 PM
Last Post: snippsat
  problem in import module from other folder akbarza 5 1,445 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  Problem with Pyinstaller. No module named '_tkinter' tonynapoli2309 0 1,021 May-15-2023, 02:38 PM
Last Post: tonynapoli2309
  Problem with module time and leap seconds Pedroski55 3 1,259 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  problem with pyinstaller to create an executable file atlass218 0 2,577 May-15-2021, 11:01 AM
Last Post: atlass218
  keyboard module; must be root problem philipbergwerf 2 19,134 Apr-04-2021, 11:40 AM
Last Post: philipbergwerf
  can't read QRcode in large file simoneek 0 1,516 Sep-16-2020, 08:52 AM
Last Post: simoneek
  qrcode detector not in cv2 Pedroski55 2 5,155 Sep-16-2020, 03:22 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020