Python Forum
Win32API Troubles - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Win32API Troubles (/thread-25248.html)



Win32API Troubles - daaaabs - Mar-24-2020

Hi, so I've had this code that caused the mouse cursor to move in a specific pattern. I ran it via Windows Powershell via the following steps:

- running Powershell as admin
- pasting '[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Users\User\AppData\Local\Programs\Python\Python36")' (outside quotes not included of course)
- typing 'python'
- pasting the actual code

It worked effectively before, then all of a sudden, without uninstalling or changing anything, I get this error:
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: name 'win32api' is not defined
Anyone know the root cause of this and how to fix this? I apologize if it's an easy fix and this is a dumb question, I'm in the extreme shallow ends when it comes to the language. I have tried reinstalling mouse/keyboard packages, win32api, and even python itself. Again, it was working just fine for the longest time, then all of a sudden this error came up. Any help would be much appreciated, thanks! Code itself is just below:
import win32api
import mouse
from time import sleep
import random
from random import randint             

# Digits
runter =[18,18,14,14,14,14,14,14,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
cunter =[-4,-4,-4,-4,-4,-4,-4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]


# vars
count = 0
EinAus = False
jaornein = randint(0,1)



# Loop
count = 0
while True:
    if win32api.GetAsyncKeyState(ord('0')):
        EinAus = not EinAus
        sleep(0.2)
        print('On')
    sleep(0.2)
    while EinAus is True:
        count = 0
        while mouse.is_pressed(button='left'):
            if count < 40:
                jaornein = randint(0,1)
                if jaornein == 1:
                    randomized = randint(0,5)
                elif jaornein == 0:
                    randomized = random.uniform(0,-5)    
                print(randomized)
                ammount = runter[count]
                drew = cunter[count]
                if jaornein == 1:
                    ammount = ammount + randomized
                    drew = drew + randomized
                elif jaornein == 0:
                    ammount = ammount - randomized
                    drew = drew - randomized
                ammountFinal = int(round(ammount))
                drewFinal = int(round(drew))
                win32api.mouse_event(0x0001,drewFinal,ammountFinal)
                sleep(0.10)    
            count = count + 1
            if count > 40:
                count = 0
                ammount = 0  
                drew = 0
        if win32api.GetAsyncKeyState(ord('0')):
            print('Out')
            EinAus = not EinAus
            sleep(0.2)
            break
        sleep(0.1)



RE: Win32API Troubles - buran - Mar-24-2020

may I ask why that complicated scheme running the code? What does "pasting actual code", where it comes from? Why not simply save the code as py files and run it the normal way?

https://python-forum.io/Thread-How-to-Execute-python-code

it looks like import win32api was changed probably by mistake - import was deleted and first line in empty, second line is just win32api. But that is just a guess based on the traceback


RE: Win32API Troubles - daaaabs - Mar-24-2020

(Mar-24-2020, 07:03 PM)buran Wrote: may I ask why that complicated scheme running the code? What does "pasting actual code", where it comes from? Why not simply save the code as py files and run it the normal way?

https://python-forum.io/Thread-How-to-Execute-python-code

it looks like import win32api was changed probably by mistake - import was deleted and first line in empty, second line is just win32api. But that is just a guess based on the traceback

I'm running it for a game, and it basically requires it to be through Powershell. I have the code in its own document, and I'd always just paste it into Powershell (after I did those previously stated steps). The one thing I neglected to mention, was what Powershell displayed in the actual code; that error I pasted is what it says below after I hit enter. Here's what it says on the actual code:
>>> import win32api
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: DLL load failed: The specified module could not be found.
>>> import mouse
>>> from time import sleep
>>> import random
>>> from random import randint
>>>
>>> # Digits
>>> runter =[18,18,14,14,14,14,14,14,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
>>> cunter =[-4,-4,-4,-4,-4,-4,-4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
>>>
>>>
>>> # vars
>>> count = 0
>>> EinAus = False
>>> jaornein = randint(0,1)
>>>
>>>
>>>
>>> # Loop
>>> count = 0
>>> while True:
...     if win32api.GetAsyncKeyState(ord('0')):
...         EinAus = not EinAus
...         sleep(0.2)
...         print('On')
...     sleep(0.2)
...     while EinAus is True:
...         count = 0
...         while mouse.is_pressed(button='left'):
...             if count < 40:
...                 jaornein = randint(0,1)
...                 if jaornein == 1:
...                     randomized = randint(0,5)
...                 elif jaornein == 0:
...                     randomized = random.uniform(0,-5)
...                 print(randomized)
...                 ammount = runter[count]
...                 drew = cunter[count]
...                 if jaornein == 1:
...                     ammount = ammount + randomized
...                     drew = drew + randomized
...                 elif jaornein == 0:
...                     ammount = ammount - randomized
...                     drew = drew - randomized
...                 ammountFinal = int(round(ammount))
...                 drewFinal = int(round(drew))
...                 win32api.mouse_event(0x0001,drewFinal,ammountFinal)
...                 sleep(0.10)
...             count = count + 1
...             if count > 40:
...                 count = 0
...                 ammount = 0
...                 drew = 0
...         if win32api.GetAsyncKeyState(ord('0')):
...             print('Out')
...             EinAus = not EinAus
...             sleep(0.2)
...             break
...         sleep(0.1)
You see, it keeps saying that the module cannot be found. Thing is, it has always been installed and it was working before. Out of the blue this issue happened. Tried reinstalling, same issue. Odd issue and I'm out of ideas on how to fix it :/