AttributeError: 'module' object has no attribute 'QRcode' - linux

For some reason I get AttributeError.
My code in Genie is:
import qrcode
import cv2
qr = qrcode.QRCode()
text=raw_input("Insert text to generate: ")
qr.add_data(text)
qr.make()
img = qr.make_image(fill_color="#000000", back_color="#ffffff")
img.save('code.png')
img = cv2.imread('code.png')
cv2.imshow('QR Code', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I want to generate a QR code through python, but I get errors.

I was able to solve this by first upgrading pip, then installing qrcode as described at PyPi (https://pypi.org/project/qrcode/)
upgrade pip
pip install --upgrade pip
use pip to install qrcode from PyPi
pip install qrcode[pil]
Note: I did these two steps in a python virtual environment for python v3.8. Later will try again with 3.11.1 and will try to circle back to this thread if/when I get that working.
I followed these instructions for my python3 virtual env: https://docs.python.org/3/tutorial/venv.html
python3 -m venv tutorial-env

Related

Module not found when I tried to import pyPDF2

My python version is 3.6. I am able to install the pyPDF2.
Ran pip install pyPDF2 successfully.
Ran pip list, it shows up as 1.26.0
My environment is not base, but I set up an environment as pytorch. pyPDF2 is installed successfully in this environment.
It pops error when I tried to import it. (typo fixed from the original post)
Your import pyPDF is incorrect. Instead try import PyPDF2.
Is also possible your pip does not match your python (this can be the case when multiple python versions are installed at the same time). Try running python -m pip list to confirm what exactly is installed.

pyttsx3.init() give an error in python 3.9

I tried below code:
>>> import pyttsx3
>>> engine = pyttsx3.init()
>>> engine.say('hello')
>>> engine.runAndWait()
But it give me an error as below:
import pywintypes
ModuleNotFoundError: No module named 'pywintypes'
I already tried this to solve:
pip install pypiwin32
python -m pip install pywin32
pip install -U pypiwin32
python -m pip install pyttsx3==2.71
But not any one work for me.
I'm using win10
can anyone help me?
If you want to use pyttsx3 in python 3 you have to use pip3 not pip to install the packages. It won't work with pip. pip works for python2 only.
Try the following:
pip3 install pywin32 pypiwin32 pyttsx3
This should work

ImportError: No module named PIL works for 3.7, but not 3.8

I have the Pillow module installed as seen on pip list. Which shows Pillow 7.2.0.
I have code which works on python 3.7.
from PIL import Image, ImageTk
However, when running the same on python 3.8 get the error message.
Exception has occurred: ModuleNotFoundError
No module named 'PIL'
I note there is supposed to be a working solution here:
stack link with potential solutions
2 solutions are:
import Image
uninstall Pillow and re-install
However, the above suggestions do not work.
How do i get Image and ImageTK working (on version 3.8) please ?
You could start a virtual environment venv or virtualenv with clean python3.8 and install modules there or try with:
python-3.8 -m pip install Pillow
or
python3.8 -m pip install Pillow
Edit:
In the end it worked with:
py -3.8 -m pip install Pillow
Hope this was useful ;)

Python3.8 pytube ImportError: cannot import name 'YouTube' from 'pytube' (unknown location)

When i try to run my script, which is just those two lines:
from pytube import YouTube
YouTube('http://youtube.com/watch?v=9bZkp7q19f0').streams.first().download()
i get the error:
ImportError: cannot import name 'YouTube' from 'pytube' (unknown location)
ive used the following documentation to install pytube:
https://github.com/NFicano/pytube
but since i am using python3.8 i installed pytube3 using:
pip3 install pytube3
instead of:
pip install pytube
which does not throw errors from the module.
When double check if there is a "pytube" module directory in:
C:\Users\User\AppData\Local\Programs\Python\Python38\Lib\site-packages
if found the directory: "\pytube" inside of it
when i use: pip install pytube3
i get the output:
Requirement already satisfied: pytube3 in c:\users\User\appdata\local\programs\python\python38\lib\site-packages (9.6.4)
Requirement already satisfied: typing-extensions in c:\users\User\appdata\local\programs\python\python38\lib\site-packages (from pytube3) (3.7.4.2)
pip uninstall pytube3
pip install pytube3
Step 2 returns the filepath where pytube3 was installed. go to the folder that was returned, open "pytube/", and clear the contents of the "pycache" folder, which has all the ".pyc" files. These are not important and can be regenerated by the package whenever it is loaded.
If all of that doesn't work try doing the following pip install pytube3 --upgrade
use this:- it works for me after upgrade your pytube
pip install pytube3 --upgrade
import pytube
myVideo = pytube.YouTube('youtube's video url')

Can't display graphviz tree in Jupyter Notebook

I'm trying to display a decision tree in Jupyter Notebook and I keep receiving the message:
CalledProcessError: Command '['dot.bat', '-Tsvg']' returned non-zero exit status 1
I'm using the following code:
from sklearn.datasets import load_iris
from sklearn import tree
import graphviz
from IPython.display import SVG
iris = load_iris()
clf = tree.DecisionTreeClassifier()
fitted_clf = clf.fit(iris.data, iris.target)
graph = graphviz.Source(tree.export_graphviz(fitted_clf,
feature_names = iris.feature_names,
class_names = iris.target_names,
filled = True, rounded = True,
special_characters = True))
SVG(graph.pipe(format='svg'))
The Exception is raised in the last line when I try to use 'pipe'.
I also tried:
graph.format = 'png'
graph.render('example')
instead of pipe but i keep on raising a similar exception:
CalledProcessError: Command '['dot.bat', '-Tpng', '-O', 'example']' returned non-zero exit status 1
Any idea of what is causing that behaviour? and how can I fix it?
(I'm using Python 3.5.2, sklearn 0.17.1, graphviz 0.8.2 and IPython 6.4.0)
Installing graphviz xorg-libxrender xorg-libxpm from conda-forge repo, and the python bindings from pip usually solves this for me.
conda install -c conda-forge graphviz xorg-libxrender xorg-libxpm
pip install graphviz
Do not forget to uninstall the previously installed packages first.
Edit: please try conda install python-graphviz instead of pip install graphviz first, as proposed in the comment below. Mixing conda and pip solved it for me multiple times, but should only be used if pure pip or conda installs fail.
Paul-Armand's answer should work if you are working with conda. If not then you have to run :
brew install graphviz
pip install graphviz
In case you get a warning saying that graphviz is already installed but not linked then follow the instruction to link it. I.e brew link graphviz (or brew link --overwrite graphviz if the former gives an error).
The reason it works in conda without brew is that conda install graphviz actually installs the c++ library not the python one.

Resources