[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

NameError: global name 'windll' is not defined

Posted on 2011-10-03
17
Medium Priority
?
3,016 Views
Last Modified: 2012-05-12
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
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()

Open in new window


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()

Open in new window


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")

Open in new window

0
Comment
Question by:dolamitejenkins
  • 12
  • 5
17 Comments
 
LVL 13

Expert Comment

by:themrrobert
ID: 36904852
Well it almost looks like your spacing is off.

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:

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()

Open in new window

0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36904861
Sorry i messed up the spacing as well:

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()

Open in new window

0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36904865
alright yea, just add 4 additional spaces before print "hi" and i think your code will work
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:dolamitejenkins
ID: 36904896
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

Open in new window

0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36904965
Can you upload the latest version of the code you are using?
0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36904991
oh duh, oops

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()

Open in new window

0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36905004
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()

Open in new window

0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36905012
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
0
 

Author Comment

by:dolamitejenkins
ID: 36905127
you mean ?

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()    

Open in new window


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 ?
0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36905709
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
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()    

Open in new window


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)
0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36905719
actually windll is from ctypes not os,
0
 
LVL 13

Accepted Solution

by:
themrrobert earned 2000 total points
ID: 36905824
basically you need to get python to recognize the ctypes library. you are already doing so in your initial script, somehow its not doing it in the 2nd. I' cannot devote any more time to this right now, hopefully you can figure it out :) i wil look at your code when you post, please only post 100% complete code as snippets will not let me help you
0
 

Author Comment

by:dolamitejenkins
ID: 36905895
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()

Open in new window




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

Open in new window

0
 

Author Closing Comment

by:dolamitejenkins
ID: 36906028
thanks for the help
0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36906043
again this is a scope issue.

either declare the variables within the scope ( under def run())

or: pass the variables like this:

kernel32.SetThreadExecutionState(c_int(master_main.ES_SYSTEM_REQUIRED|master_main.ES_CONTINUOUS|master_main.ES_DISPLAY_REQUIRED))

hope this works for you :)

(if it doesn't try including parenthesis () like this:  master_main().ES......  etc)
0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36906050
You are quite welcome :) hopefully you get the other issues fixed with this code too :p
0
 

Author Comment

by:dolamitejenkins
ID: 36906100
yes you pointed me in the right direction... it was an align of my code issue... thanks so much
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question