How to import pyHook on Spyder (Python 3.7) - python

I'm trying to use pyHook to get my image to change when I click on it with the mouse. But when I run my code, I get an error.
My Code:
from __future__ import print_function
from PIL import Image
import pyHook
import pythoncom
im = Image.open("H:/---------/Images/nature.jpg")
print(im.format, im.size, im.mode)
im.show()
def OnMouseEvent(event):
im1 = Image.open("H:/----------/Images/nature.jpg").convert("L")
im1.show()
hm = pyHook.HookManager()
hm.MouseLeft = OnMouseEvent
hm.HookMouse()
pythoncom.PumpMessages()
This is the error:
ModuleNotFoundError: No module named 'pyHook'
Screenshot:

Open up your terminal and type:
pip3 install pyHook
It is case-sensitive. So type it properly.
After that, your python environment will have pyHook installed as a module and you will be able to successfully import in your code
EDIT:
Try the following steps since you find the above did not work.
Go to https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyhook
Check your computer system and download the correct .whl file.
Open up your computer's terminal and navigate to your .whl file location. Example: cd C:\Users\ycx\Desktop
Type in: pip3 install pyHook‑1.5.1‑cp37‑cp37m‑win_amd64.whl This part should be your exact filename which you downloaded off the website.

Related

ImportError, no module named

I get ImportError: no module named Flopy when I try to run the following script from an Anaconda prompt in the folder that the script it stored in, but when I run the script through Spyder it imports Flopy just fine and the rest of the code (not shown) which uses Flopy also works.
# import the required libraries
try:
import flopy
except:
fpth = os.path.abspath(os.path.join('..', '..'))
sys.path.append(fpth)
import flopy
The Spyder ran version never runs the code under than 'except' section since it managed to import Flopy at first try. I tried checking the path created by os.path.abspath(os.path.join('..', '..')) and even copied the Flopy directory to that location and running the script from the Anaconda prompt started in that folder...which did make some difference, but the import failed with error: ImportError: cannot import name getfullargspec.
Any ideas why imports work one way but not the other?
Fixed it with help from #Jainal Patel! I just had to tidy up all my environment paths. There was an installed version of Python in C:\Python3, but Spyder was using the one in C:ProgramData\Anaconda.
Now when I open the command prompt, or an Anaconda prompt, or use Spyder, it uses the same Python environment and recognizes all my installed packages.
!pip install flopy
try:
import flopy
except:
fpth = os.path.abspath(os.path.join('..', '..'))
sys.path.append(fpth)
import flopy
you need to install it first either using pip or conda

Pillow package is required but i already have the latest version

So I'm trying to use pyautogui for a web scraper but I get this exception:
"The Pillow package is required to use this function"
I already have the latest version of Pillow
Here's the code:
import pyautogui
pyautogui.sleep(3)
pyautogui.press('win')
pyautogui.typewrite('google')
pyautogui.press('enter')
pyautogui.sleep(2)
pyautogui.typewrite('youtube.com')
pyautogui.press('enter')
coords = pyautogui.locateCenterOnScreen('new_tab.png')
pyautogui.click(coords)
Here's the line generating the error:
coords = pyautogui.locateCenterOnScreen('new_tab.png')
NOTE: I am using:
Windows 10
Python 3.9.0
VSCode
You can import Pillow at the start of your code:
import PIL
So I actually figured it out! Basically, I had to move the PIL package to site-packages, restart VSCode, and import PIL and now it works!

Why can't I import cv2 from anywhere else than install dir?

I'm using anaconda with spyder on Win 10. I installed opencv by pip-install opencv-python.
If I do
import cv2
in the default directory (C:\ProgramData\Anaconda3), it will work.
However, if I do the same command anywhere else I'll have an error :
import cv2
Traceback (most recent call last):
File "<ipython-input-3-c8ec22b3e787>", line 1, in <module>
import cv2
ImportError: DLL load failed: Le module spécifié est introuvable.
If I want to run a script which needs cv2, I have to go in C:\ProgramData\Anaconda3 , then import cv2 and finally run my script in whatever directory I want.
I don't have this problem with other modules, like pygame or pillow.
This may be because the IDE may contain many Interpreter and the cv2 library is not installed for the python Interpreter which is set for the system, try to install the cv2 with command prompt using pip install opencv-python. then it may work for you from any location.
Ok, thanks to Abhishek-Saini.
Following this video where pip directory is added to PATH, I added C:\opencv\build\x64\vc15\bin to path. And, it works ! (for now, then)

ImportError: No module named pyscreenshot

I have created I python program which makes 100 screenshots, and saves them on a folder called img inside the OS folder Documents. It worked perfectly fine in my Linux Ubuntu, it did the 100 screenshots and saved them in the directory I wanted. Now, I created a new Linux user in the same Virtual Machine, and I ran the same python script.
It gives me this error: ImportError: No module named pyscreenshot
I have tried many times. It works in my other user, although in the new one it keeps giving me error. Is there any reason for this?
Thanks. The code is below:
import os
import pyscreenshot as ImageGrab
def photos(num):
for n in range(num):
s = str(n)
a = "../Documents/img/s" + s + ".png"
# grab fullscreen
im = ImageGrab.grab()
# save image file
im.save(a)
return True
Quick note: I am calling the function photos() from another file using import screenshot (the file is called 'screenshot.py')
try to install pyscreenshot for your user
pip install pyscreenshot
Just in case the answers provided above don't work, try using
sudo -H pip install pyscreenshot

Can't save to wav from python, can't import SpeechLib from comtypes.gen, what next?

First the setup: Windows 8.1 64bit, Python 3.4 32bit.
I wanted to run the code here. So I installed comtypes
pip install comtypes
I then tried to run the code, i got
ImportError: cannot import name 'SpeechLib'
Then tried this code here to try and generate the needed SpeechLib module.
I am however still getting the same error, what should I try next?
Running these lines made it work:
from comtypes.client import CreateObject
engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")
Output was:
# Generating comtypes.gen._C866CA3A_32F7_11D2_9602_00C04F8EE628_0_5_4
# Generating comtypes.gen.SpeechLib
After this I got no import error anymore, as expected.

Categories

Resources