Dolamite Jenkins
asked on
NameError: global name 'windll' is not defined
Ok im trying to keep my program from hibernating ... found some code and I cant say I fully understand it but when I run it this way
as far as I can tell it runs fine... but from my reading the SetThreadExecutionState has to run as a thread that is called every so often(let's say q5min) so I set up my thread like so:
and I get this error ... I have to admit that I have done a ton of reading and I only minimally understand threading but why this error ? still importing all the above modules ..
from ctypes import *
import os
import time
ES_SYSTEM_REQUIRED = 0x00000001
ES_DISPLAY_REQUIRED = 0x00000002
ES_CONTINUOUS = 0x80000000
def master_main():
kernel32 = windll.LoadLibrary("Kernel32.dll")
kernel32.SetThreadExecutionState(c_int(ES_SYSTEM_REQUIRED|ES_CONTINUOUS|ES_DISPLAY_REQUIRED))
if (__name__ == "__main__"):
master_main()
as far as I can tell it runs fine... but from my reading the SetThreadExecutionState has to run as a thread that is called every so often(let's say q5min) so I set up my thread like so:
class master_main(threading.Thread):
ES_SYSTEM_REQUIRED = 0x00000001
ES_DISPLAY_REQUIRED = 0x00000002
ES_CONTINUOUS = 0x80000000
def run(self):
time.sleep(20)
kernel32 = windll.LoadLibrary("Kernel32.dll")
kernel32.SetThreadExecutionState(c_int(ES_SYSTEM_REQUIRED|ES_CONTINUOUS|ES_DISPLAY_REQUIRED))
print "hi"
master_main().start()
and I get this error ... I have to admit that I have done a ton of reading and I only minimally understand threading but why this error ? still importing all the above modules ..
NameError: global name 'windll' is not defined
File "C:\Python26\Lib\threading.py", line 504, in __bootstrap
self.__bootstrap_inner()
File "C:\Python26\Lib\threading.py", line 532, in __bootstrap_inner
self.run()
File "C:\Python26\sign\waking_up_version_Oct3.py", line 6769, in run
kernel32 = windll.LoadLibrary("Kernel32.dll")
Sorry i messed up the spacing as well:
hopefully this uploads proper
hopefully this uploads proper
class master_main(threading.Thread):
ES_SYSTEM_REQUIRED = 0x00000001
ES_DISPLAY_REQUIRED = 0x00000002
ES_CONTINUOUS = 0x80000000
def run(self):
time.sleep(20)
kernel32 = windll.LoadLibrary("Kernel32.dll")
kernel32.SetThreadExecutionState(c_int(ES_SYSTEM_REQUIRED|ES_CONTINUOUS|ES_DISPLAY_REQUIRED))
print "hi"
master_main().start()
alright yea, just add 4 additional spaces before print "hi" and i think your code will work
ASKER
Ran it and I got this error ?
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Python26\lib\threading.py", line 532, in __bootstrap_inner
self.run()
File "C:\Python26\sign\waking_up_version_Oct3.py", line 6771, in run
kernel32 = windll.LoadLibrary("Kernel32.dll")
NameError: global name 'windll' is not defined
Can you upload the latest version of the code you are using?
oh duh, oops
here try this:
here try this:
from ctypes import *
import os
import time
ES_SYSTEM_REQUIRED = 0x00000001
ES_DISPLAY_REQUIRED = 0x00000002
ES_CONTINUOUS = 0x80000000
class master_main(threading.Thread):
ES_SYSTEM_REQUIRED = 0x00000001
ES_DISPLAY_REQUIRED = 0x00000002
ES_CONTINUOUS = 0x80000000
def run(self):
time.sleep(20)
kernel32 = windll.LoadLibrary("Kernel32.dll")
kernel32.SetThreadExecutionState(c_int(ES_SYSTEM_REQUIRED|ES_CONTINUOUS|ES_DISPLAY_REQUIRED))
print "hi"
master_main().start()
or this to keep it all together: ( i wish EE had a edit option >.<)
class master_main(threading.Thread):
from ctypes import *
import os
import time
ES_SYSTEM_REQUIRED = 0x00000001
ES_DISPLAY_REQUIRED = 0x00000002
ES_CONTINUOUS = 0x80000000
def run(self):
time.sleep(20)
kernel32 = windll.LoadLibrary("Kernel32.dll")
kernel32.SetThreadExecutionState(c_int(ES_SYSTEM_REQUIRED|ES_CONTINUOUS|ES_DISPLAY_REQUIRED))
print "hi"
master_main().start()
if my last code fails, the last thing you can try is adding master_main. before the windll call
windll comes from the os library so if you don't include it in your class you won't be able to access the windll.LoadLibrary() function.
Sorry I should have caught that earlier
windll comes from the os library so if you don't include it in your class you won't be able to access the windll.LoadLibrary() function.
Sorry I should have caught that earlier
ASKER
you mean ?
Im sorry Im relatively new to programming so I don't understand " if you don't include it in your class you won't be able to access the windll.LoadLibrary() function. " means ... I called import os and import ctypes at the very beginning of my program ... so should I include it again ?
class master_main(threading.Thread):
kernel32 = windll.LoadLibrary("Kernel32.dll")
def run(self):
time.sleep(20) kernel32.SetThreadExecutionState(c_int(ES_SYSTEM_REQUIRED|ES_CONTINUOUS|ES_DISPLAY_REQUIRED))
print "hi"
master_main().start()
Im sorry Im relatively new to programming so I don't understand " if you don't include it in your class you won't be able to access the windll.LoadLibrary() function. " means ... I called import os and import ctypes at the very beginning of my program ... so should I include it again ?
No you don't need to call it again, I was basing my answer on the 2nd block of you posted. It looked like when you made the 2nd program you removed the import os lines
Since python uses tabs and spaces to define the body of f unctions, it is EXTREMELY IMPORTANT to be consistent with spacing.
This means, in your latest posted code for example, you must keep the spacing consistent. Notice how in this code the spacing convention is consistent. this makes it much easier to debug, troubleshoot and read
Basically the way your code works, is it calls native win32 libraries (kernel32). The error you are getting says that in the current scope (scope is similar to the layer that is currently being executed) windll is not defined. This can happen if the library holding windll isn't showing up.
Can you post YOUR ENTIRE file, not just a snippet of code? You said you already imported os, but in your original post i couldn't find it (except in the first example which you said worked fine)
Since python uses tabs and spaces to define the body of f unctions, it is EXTREMELY IMPORTANT to be consistent with spacing.
This means, in your latest posted code for example, you must keep the spacing consistent. Notice how in this code the spacing convention is consistent. this makes it much easier to debug, troubleshoot and read
class master_main(threading.Thread):
kernel32 = windll.LoadLibrary("Kernel32.dll")
def run(self):
time.sleep(20) kernel32.SetThreadExecutionState(c_int(ES_SYSTEM_REQUIRED|ES_CONTINUOUS|ES_DISPLAY_REQUIRED))
print "hi"
master_main().start()
Basically the way your code works, is it calls native win32 libraries (kernel32). The error you are getting says that in the current scope (scope is similar to the layer that is currently being executed) windll is not defined. This can happen if the library holding windll isn't showing up.
Can you post YOUR ENTIRE file, not just a snippet of code? You said you already imported os, but in your original post i couldn't find it (except in the first example which you said worked fine)
actually windll is from ctypes not os,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
fixed the first error and now I have this one th current code
from ctypes import *
class master_main(threading.Thread):
ES_SYSTEM_REQUIRED = 0x00000001
ES_DISPLAY_REQUIRED = 0x00000002
ES_CONTINUOUS = 0x80000000
def run(self):
time.sleep(20)
kernel32 = windll.LoadLibrary("Kernel32.dll")
kernel32.SetThreadExecutionState(c_int(ES_SYSTEM_REQUIRED|ES_CONTINUOUS|ES_DISPLAY_REQUIRED))
print "hi"
master_main().start()
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Python26\lib\threading.py", line 532, in __bootstrap_inner
self.run()
File "C:\Python26\sign\waking_up_version_Oct3.py", line 6777, in run
kernel32.SetThreadExecutionState(c_int(ES_SYSTEM_REQUIRED|ES_CONTINUOUS|ES_DISPLAY_REQUIRED))
NameError: global name 'ES_SYSTEM_REQUIRED' is not defined
ASKER
thanks for the help
again this is a scope issue.
either declare the variables within the scope ( under def run())
or: pass the variables like this:
kernel32.SetThreadExecutio nState(c_i nt(master_ main.ES_SY STEM_REQUI RED|master _main.ES_C ONTINUOUS| master_mai n.ES_DISPL AY_REQUIRE D))
hope this works for you :)
(if it doesn't try including parenthesis () like this: master_main().ES...... etc)
either declare the variables within the scope ( under def run())
or: pass the variables like this:
kernel32.SetThreadExecutio
hope this works for you :)
(if it doesn't try including parenthesis () like this: master_main().ES...... etc)
You are quite welcome :) hopefully you get the other issues fixed with this code too :p
ASKER
yes you pointed me in the right direction... it was an align of my code issue... thanks so much
Be sure to use EITHER a tab or 4 spaces for each indent.
This can cause scope issues which can produce unexpected results.
Try this code and upload the output please:
Open in new window