Automate whatsapp using python - python

I'm trying to automate whatsapp using python in my android phone. I'm using pydroid 3 ide to do it.
From the terminal I installed pywhatkit
$ pip install pywhatkit
And i logged into whatsapp web from chrome browser.
Now I've write the script named test.py like this -
import pywhatkit
pywhatkit.sendwhatmsg("+9176xxxxxx18","hello","10,36")
(My number imstead of +9176xxxxxxxx18)
(I've set the time 2 minutes later from my time)
But whenever I run the script it throws an error in importing pywhatkit
Something like this -
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 1, in <module>
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/pywhatkit/__init__.py", line 8, in <module>
from pywhatkit.main import print_sleep_time, check_window, sendwhatmsg, sendwhatmsg_to_group, info, playonyt, search, close_tab, sendwhatmsg_instantly, sendwhats_image
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/pywhatkit/main.py", line 9, in <module>
import pyautogui as pg
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/pyautogui/__init__.py", line 249, in <module>
import mouseinfo
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/mouseinfo/__init__.py", line 223, in <module>
_display = Display(os.environ['DISPLAY'])
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/os.py", line 675, in __getitem__
raise KeyError(key) from None
KeyError: 'DISPLAY'
[Program finished]
Image :- enter image description here
I can't understand why I'm getting this error please help me.let me know if there any other way to send whatsapp messege using python on android? I also have a terminal termux on my android which runs on linux into bash [my desi terminal 😂]

I see you are trying to run from mobile[android] device, this module requires PC browser. So it wont be able to run in your mobile. Try running test.py through your PC. And you cant send whatsapp messege using python on android.

Related

How to fix "ImportError : This platform not supported" when using "pystray" with Pyinstaller?

So after I compiled the program I wrote using pyinstaller in one-file mode, The program doesn't seem to function at all.
When I try to execute it, I receive the following Exception in a Window:
Traceback (most recent call last):
File "main.py", line 6, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pystray\__init__.py", line 48, in <module>
File "pystray\__init__.py", line 44, in backend
ImportError: this platform is not supported: No module named 'pystray._win32'
What could be the issue here? I mean how do I solve this problem?
I'm currently running:
Python 3.9
Windows 10 (the latest version)
Visual Studio Code
Try using the git bash or command prompt. I don't think PyInstaller works with the VSC terminal.

Pyinstaller having issues with import of librairies

I was working with python (which I rarely do) for a simple program that macros some functionalities with certain keyboard inputs. I decided to export the script to a .exe using pyinstaller so I go ahead and run command: pyinstaller --onefile code.py and it generates the code.exe as expected. Only for my .exe to crash instantly. I got a view of the error by running it with cmd and got this
Traceback (most recent call last):
File "code.py", line 1, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pynput\__init__.py", line 40, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pynput\keyboard\__init__.py", line 31, in <module>
File "pynput\_util\__init__.py", line 76, in backend
ImportError
[1284] Failed to execute script 'code' due to unhandled exception!
I'm certain there is an explanation to this so here are my imports:
from pynput import keyboard
from pynput.keyboard import Key
from pywinauto import application
from pywinauto.findwindows
import WindowAmbiguousError, WindowNotFoundError
import os
I couldn't seem to find any similar problems on here. Thank you in advance for the help.
In your .spec file, you should add all of your modules as "hidden imports", this usually fixes all module not found errors.
hiddenimport=["pynput","pywinauto"]
Then run pyinstaller with the spec file
pyinstaller my.spec
I'd also ensure you're using a virtual environment during this entire process.

importing pywinauto.application throws error while trying to run using python 3.5.4

I was trying to execute the following command on windows 10 machine using python 3.5.4,
from pywinauto.application import Application
Following is the error which i was receiving
Traceback (most recent call last):
File "C:/Users/bizact/Desktop/PSAV/test.py", line 1, in <module>
from pywinauto.application import Application
File "C:\Program Files\Python35\lib\site-packages\pywinauto\__init__.py", line 72, in <module>
from . import findwindows
File "C:\Program Files\Python35\lib\site-packages\pywinauto\findwindows.py", line 42, in <module>
from . import controls
File "C:\Program Files\Python35\lib\site-packages\pywinauto\controls\__init__.py", line 36, in <module>
from . import uiawrapper # register "uia" back-end (at the end of uiawrapper module)
File "C:\Program Files\Python35\lib\site-packages\pywinauto\controls\uiawrapper.py", line 46, in <module>
from ..uia_defines import IUIA
File "C:\Program Files\Python35\lib\site-packages\pywinauto\uia_defines.py", line 35, in <module>
import comtypes.client
File "C:\Program Files\Python35\lib\site-packages\comtypes\client\__init__.py", line 33, in <module>
gen_dir = _find_gen_dir()
File "C:\Program Files\Python35\lib\site-packages\comtypes\client\_code_cache.py", line 71, in _find_gen_dir
result = os.path.abspath(gen_path[-1])
IndexError: list index out of range
Is there a way i can fix this ?
Try running the script with administrative privileges. Just by looking at the traceback it looks like some cached files that pywinauto import tried to write but later on it wasn't found, and generally the script can't write the file due to permissions. If the above don't work, try installing python somewhere else (C:/Python with full privileges in order to isolate the issue)
This is comtypes issue. It will be fixed soon (EDIT: fixed in comtypes==1.1.7). You can downgrade to one of previous versions: pip install comtypes==1.1.2. Or run script as Administrator as Rodolfo suggested (disabling UAC may not help, need to say explicitly to "run as Administrator" by popup menu).

Pywinauto app = Application.start() is not working and giving error

I get an error when I try to start an exe file with pywinauto. This is my code:
from pywinauto.application import Application
app = Application()
app.Start("D:\dragonboy160\dragonboy160.exe")
As far as I know that should work. app = Application().start("") does not work either. I also tried setting the imports like this: from pywinauto import Application I get this error when I run the python file in PyCharm:
"C:\Program Files (x86)\python.exe" D:/dragonboy160/screenview.py
Traceback (most recent call last):
File "D:/dragonboy160/screenview.py", line 2, in <module>
import pywinauto
File "C:\Program Files (x86)\lib\site-packages\pywinauto\__init__.py", line 40, in <module>
from . import findwindows
File "C:\Program Files (x86)\lib\site-packages\pywinauto\findwindows.py", line 42, in <module>
from . import controls
File "C:\Program Files (x86)\lib\site-packages\pywinauto\controls\__init__.py", line 36, in <module>
from . import uiawrapper # register "uia" back-end (at the end of uiawrapper module)
File "C:\Program Files (x86)\lib\site-packages\pywinauto\controls\uiawrapper.py", line 44, in <module>
from ..uia_defines import IUIA
File "C:\Program Files (x86)\lib\site-packages\pywinauto\uia_defines.py", line 35, in <module>
import comtypes.client
File "C:\Program Files (x86)\lib\site-packages\comtypes\client\__init__.py", line 33, in <module>
gen_dir = _find_gen_dir()
File "C:\Program Files (x86)\lib\site-packages\comtypes\client\_code_cache.py", line 71, in _find_gen_dir
result = os.path.abspath(gen_path[-1])
IndexError: list index out of range
I searched and searched and it seems no other person has had this problem. I also tried opening other .exe files but got the same error. What is causing this error and how can I fix it?
This is an import problem (at the very first line).
This looks like comtypes can't write cached files to Program files without admin privileges. In theory it might be fixed on comtypes side (or by re-installing Python to another folder like C:\Python3x), but you will have to run the script as Administrator anyway because of other OS restrictions (pywinauto will warn about insufficient rights in nearest future, now it's silent).
I had the same problem and I managed to have it fixed by disabling UAC from Windows.

Starting Sikuli from Python returns error

I want to run Sikuli for automating windows application using Python. I am new to Sikuli. Actually I have installed Sikuli by using pip install sikuli. And then I tried to import it in my Python file as given below:
from sikuli import *
print("Working")
but when i run it using 'python sikuliTest.py', I get the following error as given below:
Traceback (most recent call last):
File "sikuliTest.py", line 1, in <module>
from sikuli import *
File "c:\Python27\lib\site-packages\sikuli-0.1-py2.7- win32.egg\sikuli\__init__
.py", line 1, in <module>
from sikuli import *
File "c:\Python27\lib\site-packages\sikuli-0.1-py2.7- win32.egg\sikuli\sikuli.p
y", line 15, in <module>
from jnius import autoclass
File "d:\users\deepak-ra\appdata\local\temp\easy_install-4xytd2\jnius- 1.1.dev0
-py2.7-win32.egg.tmp\jnius\__init__.py", line 12, in <module>
File "d:\users\deepak-ra\appdata\local\temp\easy_install-4xytd2\jnius- 1.1.dev0
-py2.7-win32.egg.tmp\jnius\jnius.py", line 7, in <module>
File "d:\users\deepak-ra\appdata\local\temp\easy_install-4xytd2\jnius-1.1.dev0
-py2.7-win32.egg.tmp\jnius\jnius.py", line 6, in __bootstrap__
ImportError: DLL load failed: The specified module could not be found.
Am not sure whether I had installed it correctly or where I have done the mistake. Please help me out with this.
Sikuli is using Jython and cannot be run directly from Python. If you want to use Python syntax to create scripts using Sikuli start reading here. Have a look at section 3 "How do I set up SikuliX".
For any specific questions/issues use the Sikuli Launchpad.

Categories

Resources