Posts: 142
Threads: 68
Joined: Jul 2023
Aug-19-2023, 02:57 PM
(This post was last modified: Aug-19-2023, 03:09 PM by Yoriz.
Edit Reason: Added code tags
)
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.
Posts: 2,168
Threads: 35
Joined: Sep 2016
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/
Posts: 142
Threads: 68
Joined: Jul 2023
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
Posts: 12,041
Threads: 487
Joined: Sep 2016
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.
Posts: 1,094
Threads: 143
Joined: Jul 2017
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.
Posts: 7,324
Threads: 123
Joined: Sep 2016
# 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
Posts: 142
Threads: 68
Joined: Jul 2023
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
Posts: 7,324
Threads: 123
Joined: Sep 2016
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
Posts: 142
Threads: 68
Joined: Jul 2023
(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
Posts: 7,324
Threads: 123
Joined: Sep 2016
(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'
|