as the title says, python 3.10 doesn't find GetWindowThreadProcessId()
in the win32process
library. I've taken a while to search for this error but it seems to work for everyone else.
I am running my code within a python 3.10 venv created by pycharm. I installed the Libraries today from within the venv.
My program is the following (it tries to get some details of the current foreground window):
import win32gui
import win32process
import psutil
import time
from datetime import datetime
last_window = None
while True:
try:
hwnd = win32gui.GetForegroundWindow()
if hwnd != last_window:
last_window = hwnd
# here is the problem, this doesn't deem to exist for my python
tid, pid = win32process.GetWindowThreadProcessId(hwnd)
proc = psutil.Process(pid)
window_title = win32gui.GetWindowText(hwnd)
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"[{timestamp}] Focus switched to PID {pid} ({proc.name()}) - '{window_title}'")
except Exception as e:
print(f"Error: {e}")
time.sleep(0.005) # checks every 50ms
Edit: Upon request, I removed the try/except and got the following stacktrace:
Traceback (most recent call last):
File "C:\<blah>\main.py", line 14, in <module>
tid, pid = win32process.GetWindowThreadProcessId(hwnd)
AttributeError: module 'win32process' has no attribute 'GetWindowThreadProcessId'
Process finished with exit code 1
This indicates that I don't even get a pid or tid readout.
I'm using https://files.pythonhosted.org/packages/aa/fe/d873a773324fa565619ba555a82c9dabd677301720f3660a731a5d07e49a/pywin32-310-cp310-cp310-win_amd64.whl
at the moment.
About the postinstall script:
I've read somewhere (lost the link, sorry) that this shouldn't be executed in a venv...
Okay, so after some testing, it boiled down to the python Interpreter being a (in?) Python venv.
I have now installed the same version of python interpreter and libraries system-wide and the Program works. (until it doesn't, which is a thing for another round of research)
--> Therefore, do not expect libraries like pywin32, which go into operating system stuff to fully work inside a venv.
Thanks to the few who tried to help!