Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Win32API Troubles
#1
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)
Reply
#2
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-Ex...ython-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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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-Ex...ython-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 :/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  troubles with tqdm max22 2 486 Nov-27-2023, 09:20 PM
Last Post: max22
  Installation troubles on Win 10 peringek 1 2,888 Dec-31-2020, 07:30 AM
Last Post: caleb_cruze
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 3,728 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  converting user input to float troubles RecklessTechGuy 3 2,406 Aug-17-2020, 12:41 PM
Last Post: deanhystad
  csv troubles DPaul 4 2,283 Aug-13-2020, 05:18 PM
Last Post: DPaul
  local variable troubles yokaso 4 3,160 Oct-20-2019, 05:25 PM
Last Post: ichabod801
  how do i fix “No module named win32api” on python2.7 ? shongr2001 1 12,843 Apr-18-2019, 04:03 PM
Last Post: snippsat
  How to import win32api and win32con SheeppOSU 2 25,541 Feb-19-2019, 12:04 AM
Last Post: snippsat
  Troubles with instaling pocketsphinx Thais781 1 2,606 Aug-07-2018, 10:05 AM
Last Post: Larz60+
  Troubles with classes, taken from a book sylas 2 3,159 Jun-05-2017, 08:39 AM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020