By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

compiling to myProgram.exe from myProgram.pyw

P: 10
Hi!!!

I need some help:

I have a program: myProgram.pyw, and I want it like myProgram.exe so anyone can run it in a windows xp without installing nothing.

I've read how py2exe works and I undersand it when is a simple program.

myProgram.pyw uses ImageGrab module and pyHook module.

How I can create myProgram.exe that "include" that modules and run with no problems¿??

Thanks!!!!
May 22 '07 #1
Share this Question
Share on Google+
20 Replies


bartonc
Expert 5K+
P: 6,596
Hi!!!

I need some help:

I have a program: myProgram.pyw, and I want it like myProgram.exe so anyone can run it in a windows xp without installing nothing.

I've read how py2exe works and I undersand it when is a simple program.

myProgram.pyw uses ImageGrab module and pyHook module.

How I can create myProgram.exe that "include" that modules and run with no problems¿??

Thanks!!!!
I've been working with Py2Exe and have learned some tricks. I have posted here (which is NOT for Q&A) and other placed also in the Articles Section.

Do some work on you setup file and post back IN THIS THREAD.

Thanks for joining.
May 22 '07 #2

P: 10
I've been working with Py2Exe and have learned some tricks. I have posted here (which is NOT for Q&A) and other placed also in the Articles Section.

Do some work on you setup file and post back IN THIS THREAD.

Thanks for joining.
Hi! sorry for PM you, I didn't know about the rules.


Here I have my simple program: a screen shot taker:
Expand|Select|Wrap|Line Numbers
  1. import ImageGrab,pythoncom,pyHook,os
  2. from datetime import *
  3. def OnKeyboardEvent(event):
  4.    if(event.Key=="F7"):
  5.       #catching the f7 event
  6.       s=getDateInString()
  7.       aux=r"C:\\ScreenShots\\ss"
  8.       path=aux+s+".jpg"
  9.       print path
  10.       img = ImageGrab.grab()
  11.       img.save(path)
  12.    return True
  13. def getDateInString():
  14.    #tunning the path
  15.    s=str(datetime.today()).split(' ')
  16.    return s[0]+'-'+s[1].replace(':','-').split('.')[0]
  17. # if dont exist, create directory where all ss will be saved
  18. if(os.access("C:\\ScreenShots",os.F_OK)==False):
  19.    os.mkdir("C:\\ScreenShots")
  20. # create a hook manager
  21. hm = pyHook.HookManager()
  22. # watch for all mouse events
  23. hm.KeyDown = OnKeyboardEvent
  24. # set the hook
  25. hm.HookKeyboard()
  26. # wait forever
  27. pythoncom.PumpMessages()
  28.  


it's named sstaker.pyw.

the ImageGrab module is Python Imaging Library 1.1.6 for Python 2.5 (Windows only) on http://www.pythonware.com/products/pil/index.htm

the pyHook module is http://sourceforge.net/project/downl...5.exe&37280531
for windows too.

os and datetime comes with python.

If only I could get a sstaker.exe which I can pass anyone with windows xp without that person installs nothing previously.
May 23 '07 #3

P: 10
oh :( At least tell me if it is possible to do, because I'm trying to get it for more than one month asking in a lot of forums and reading tutorials...
May 24 '07 #4

bartonc
Expert 5K+
P: 6,596
oh :( At least tell me if it is possible to do, because I'm trying to get it for more than one month asking in a lot of forums and reading tutorials...
Sorry, Freddy. My connections been wonky all day. It is possible and probably even easy. Give me a little bit to get to this.

Thanks.
May 24 '07 #5

bartonc
Expert 5K+
P: 6,596
Sorry, Freddy. My connections been wonky all day. It is possible and probably even easy. Give me a little bit to get to this.

Thanks.
Re-subscribing and bumping...
May 24 '07 #6

bartonc
Expert 5K+
P: 6,596
Hi! sorry for PM you, I didn't know about the rules.


Here I have my simple program: a screen shot taker:
Expand|Select|Wrap|Line Numbers
  1. import ImageGrab,pythoncom,pyHook,os
  2. from datetime import *
  3. def OnKeyboardEvent(event):
  4.    if(event.Key=="F7"):
  5.       #catching the f7 event
  6.       s=getDateInString()
  7.       aux=r"C:\\ScreenShots\\ss"
  8.       path=aux+s+".jpg"
  9.       print path
  10.       img = ImageGrab.grab()
  11.       img.save(path)
  12.    return True
  13. def getDateInString():
  14.    #tunning the path
  15.    s=str(datetime.today()).split(' ')
  16.    return s[0]+'-'+s[1].replace(':','-').split('.')[0]
  17. # if dont exist, create directory where all ss will be saved
  18. if(os.access("C:\\ScreenShots",os.F_OK)==False):
  19.    os.mkdir("C:\\ScreenShots")
  20. # create a hook manager
  21. hm = pyHook.HookManager()
  22. # watch for all mouse events
  23. hm.KeyDown = OnKeyboardEvent
  24. # set the hook
  25. hm.HookKeyboard()
  26. # wait forever
  27. pythoncom.PumpMessages()
  28.  


it's named sstaker.pyw.

the ImageGrab module is Python Imaging Library 1.1.6 for Python 2.5 (Windows only) on http://www.pythonware.com/products/pil/index.htm

the pyHook module is http://sourceforge.net/project/downl...5.exe&37280531
for windows too.

os and datetime comes with python.

If only I could get a sstaker.exe which I can pass anyone with windows xp without that person installs nothing previously.
Your post is very complete and allowed me to get you program up and running in no time. Your program is perhaps a little too simple (no way to kill it other than T.M.) Seems like it needs some work to make sure that it doesn't steal F7 from any other running programs, also. I'll run it through py2exe, though (but don't have a lot of time at the moment).

I am curious for the need for such a program when windows will capture the screen to the clipboard which give users a chance to select format and directory (features that would be a great addition to your program).
May 25 '07 #7

P: 10
Your post is very complete and allowed me to get you program up and running in no time. Your program is perhaps a little too simple (no way to kill it other than T.M.) Seems like it needs some work to make sure that it doesn't steal F7 from any other running programs, also. I'll run it through py2exe, though (but don't have a lot of time at the moment).

I am curious for the need for such a program when windows will capture the screen to the clipboard which give users a chance to select format and directory (features that would be a great addition to your program).

Hi, you are right, my program is not very useful, but is the first program I do that interaction with the operating system, and I wanted that do a simple task like that.

The main utility of this little program is that saves the picture with the name year-month-day-hour-min-sec in jpg in a directory every time the user press F7. I know the user could do that with the Impr pant key but he would have to open Paint and paste and save it on jpg and put a name and select the directory (maybe there is another form, will be great if you could tell me about). And the idea is, that if I'm playing a game (not counter strike or so that have a lot of video recorders, a simple and not famous game) I can be playing and taking the screen shots just pressing F7 the times I want.

Anyway, will be great if you could help me improving my little program (I've been thinking in a system tray icon or so too) but will be great if I knew how to make an exe of my programs because most people wont install python and all the modules just for see how my little program works.

Thank you very much!
May 25 '07 #8

bartonc
Expert 5K+
P: 6,596
Hi, you are right, my program is not very useful, but is the first program I do that interaction with the operating system, and I wanted that do a simple task like that.

The main utility of this little program is that saves the picture with the name year-month-day-hour-min-sec in jpg in a directory every time the user press F7. I know the user could do that with the Impr pant key but he would have to open Paint and paste and save it on jpg and put a name and select the directory (maybe there is another form, will be great if you could tell me about). And the idea is, that if I'm playing a game (not counter strike or so that have a lot of video recorders, a simple and not famous game) I can be playing and taking the screen shots just pressing F7 the times I want.

Anyway, will be great if you could help me improving my little program (I've been thinking in a system tray icon or so too) but will be great if I knew how to make an exe of my programs because most people wont install python and all the modules just for see how my little program works.

Thank you very much!
Very well said, Freddy. I'll make some time this weekend for the py2exe part (I need the practice anyway). Then we can work on a more freindly "environment" for your utility to live in.
May 25 '07 #9

P: 10
Very well said, Freddy. I'll make some time this weekend for the py2exe part (I need the practice anyway). Then we can work on a more freindly "environment" for your utility to live in.

Thanks! man you rock!!
May 25 '07 #10

bartonc
Expert 5K+
P: 6,596
Thanks! man you rock!!
Here's one thing that I did to make your program more managable:
Change the extention to .py so that the python stdout/stderr window comes up when you double-click the file (if you are still needing the command-line, we can adjust your system). That way, at least the program can be killed without using Task Manager.
May 25 '07 #11

P: 10
Here's one thing that I did to make your program more managable:
Change the extention to .py so that the python stdout/stderr window comes up when you double-click the file (if you are still needing the command-line, we can adjust your system). That way, at least the program can be killed without using Task Manager.
Thanks, that was how I had it before changing the extension to .pyw. I like that a program runs without the console behind him. I was thinking and googleing for some icon in the system tray (down right on windows xp, where emule, messenger are) seems like PyQt can do it. Do you know something about?

Will be so well-looked an icon in the system try with the right-mouse button opcion "close" or something like that.

What do you think? Thanks!
May 25 '07 #12

bartonc
Expert 5K+
P: 6,596
Thanks, that was how I had it before changing the extension to .pyw. I like that a program runs without the console behind him. I was thinking and googleing for some icon in the system tray (down right on windows xp, where emule, messenger are) seems like PyQt can do it. Do you know something about?

Will be so well-looked an icon in the system try with the right-mouse button opcion "close" or something like that.

What do you think? Thanks!
My GUI toolkit is wxPython. It can do system tray icons, but I haven't tried it yet. I really like the idea, though.
May 25 '07 #13

P: 10
Maybe if I make a package with the classes that are involved in my little program (I'm thinking like a .jar in java, I don't know how it works in python) would be easier get my "sstaker.exe"?
May 26 '07 #14

bartonc
Expert 5K+
P: 6,596
Maybe if I make a package with the classes that are involved in my little program (I'm thinking like a .jar in java, I don't know how it works in python) would be easier get my "sstaker.exe"?
Well, Freddy, the ball's in your court now. I've worked on this for as long as I can. Sorry for screwing up the names. This setup script (when run in the same directory as you program will compile your sstaker.exe (I called it ImageGrabber) on line 20. For some reason (maybe this won't work at all) the events are not being registered with pyHook. The pumpmessages() seems to be working since the program doesn't exit immediately. I think that if you track down the "missing modules" error by playing with "includes" and "excludes" lists, you may have success. Have fun!
Expand|Select|Wrap|Line Numbers
  1. # Py2Exe version 6.3 setup file for wxPython GUI programs.
  2. # Creates a single exe file.
  3. # It's easiest to add this wxPython2Exe.py file into the same
  4. # folder with the source file and an optional iconfile like "icon.ico"
  5. # (if you add your own icon file, remove the comment in front of icon_resources).
  6. # Simply change the filename to whatever you called your source file.
  7. # Optionally edit the version info and add the name of your icon file.
  8. # Now run wxPython2Exe.py ...
  9. # Two subfolders will be created called build and dist.
  10. # The dist folder contains your .exe file, MSVCR71.dll and w9xpopen.exe
  11. # Your .exe file contains your code, all neded modules and the Python interpreter.
  12. # The MSVCR71.dll can be distributed, but is often already in the system32 folder.
  13.  
  14. from distutils.core import setup
  15. import py2exe
  16. import sys
  17.  
  18.  
  19. # enter the filename of your wxPython code file to compile ...
  20. filename = "ImageGrabber.py"
  21.  
  22. # ... this creates the filename of your .exe file in the dist folder
  23. if filename.endswith(".py"):
  24.     distribution = filename[:-3]
  25. elif filename.endswith(".pyw"):
  26.     distribution = filename[:-4]
  27.  
  28.  
  29. # if run without args, build executables in quiet mode
  30. if len(sys.argv) == 1:
  31.     sys.argv.append("py2exe")
  32.     sys.argv.append("-q")
  33.  
  34. class Target:
  35.     def __init__(self, **kw):
  36.         self.__dict__.update(kw)
  37.         # for the versioninfo resources, edit to your needs
  38.         self.version = "0.0.1"
  39.         self.company_name = "freddypyther"
  40.         self.copyright = "no copyright"
  41.         self.name = "Background Image Grabber"
  42.  
  43. ################################################################
  44. # The manifest will be inserted as resource into your .exe.  This
  45. # gives the controls the Windows XP appearance (if run on XP ;-)
  46. #
  47. # Another option would be to store it in a file named
  48. # test_wx.exe.manifest, and copy it with the data_files option into
  49. # the dist-dir.
  50. #
  51. manifest_template = '''
  52. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  53. <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  54. <assemblyIdentity
  55.     version="5.0.0.0"
  56.     processorArchitecture="x86"
  57.     name="%(prog)s"
  58.     type="win32"
  59. />
  60. <description>%(prog)s Program</description>
  61. <dependency>
  62.     <dependentAssembly>
  63.         <assemblyIdentity
  64.             type="win32"
  65.             name="Microsoft.Windows.Common-Controls"
  66.             version="6.0.0.0"
  67.             processorArchitecture="X86"
  68.             publicKeyToken="6595b64144ccf1df"
  69.             language="*"
  70.         />
  71.     </dependentAssembly>
  72. </dependency>
  73. </assembly>
  74. '''
  75.  
  76. RT_MANIFEST = 24
  77.  
  78. # description is the versioninfo resource
  79. # script is the wxPython code file
  80. # manifest_template is the above XML code
  81. # distribution will be the exe filename
  82. # icon_resource is optional, remove any comment and give it an iconfile you have
  83. #   otherwise a default icon is used
  84. # dest_base will be the exe filename
  85. test_grabber = Target(
  86.     description = "A non-GUI app",
  87.     script = filename,
  88.     other_resources = [(RT_MANIFEST, 1, manifest_template % dict(prog=distribution))],
  89.     #icon_resources = [(1, "icon.ico")],
  90.     dest_base = distribution)
  91.  
  92. ################################################################
  93. excludes = ["pywin.debugger", "pywin.debugger.dbgcon",
  94.             "pywin.dialogs", "pywin.dialogs.list",
  95.             "Tkconstants","Tkinter","tcl",
  96.             ] # "pywin",
  97. includes = ['PIL._imaging', 'pyHook.cpyHook']
  98.  
  99. setup(
  100.     # Need a console to be able to kill this non-GUI deamon #
  101. ##    windows = [test_grabber,],
  102. ##    console = [filename],
  103.     console = [test_grabber],
  104.     options = {"py2exe": {"compressed": 1,
  105.                           "optimize": 2,
  106.                           "ascii": 1,
  107.                           "bundle_files": 1,
  108.                           "packages":["PIL", "pyHook",],
  109.                           "excludes":excludes,
  110.                           "includes":includes}}, #
  111.     zipfile = None,
  112.     )
  113.  
May 27 '07 #15

bartonc
Expert 5K+
P: 6,596
By the way, here's my ImageGrabber.py:
Expand|Select|Wrap|Line Numbers
  1. """ImageGrabber.py: Grabs a screenshot and save it to a file with a dated name."""
  2.  
  3. import ImageGrab, pythoncom, pyHook, os
  4. from datetime import *
  5.  
  6. if(os.access("C:\\ScreenShots",os.F_OK)==False):
  7.     # if dont exist, create directory where all ss will be saved
  8.     os.mkdir("C:\\ScreenShots")
  9.  
  10. def getDateInString():
  11.     #tunning the path
  12.     s = str(datetime.today()).split(' ')
  13.     return s[0]+'-'+s[1].replace(':', '-').split('.')[0]
  14.  
  15. def OnKeyboardEvent(event):
  16.     print "Got a keyboard event"
  17.     if(event.Key == "F7"):
  18.         #catching the f7 event
  19.         s = getDateInString()
  20.         aux = r"D:\\ScreenShots\\ss"
  21.         path = aux + s + ".jpg"
  22.         print path
  23.         img = ImageGrab.grab()
  24.         img.save(path)
  25.     return True
  26.  
  27. print "Setting up the Grabber"
  28. # create a hook manager
  29. hm = pyHook.HookManager()
  30. # watch for all KeyDown events
  31. print hm
  32. hm.SubscribeKeyDown(OnKeyboardEvent)
  33. # set the hook
  34. hm.HookKeyboard()
  35. # wait forever
  36. pythoncom.PumpMessages()
  37.  
May 27 '07 #16

P: 10
OH!! thank you very much man!!! let's see if I could end this project up.
May 28 '07 #17

P: 10
Well, I've had some errors trying to run successfully the setup.py, some windows DLL and some quite difficult stuff for me. I'm so grateful for your help. I'm wondering that if the problem is with the module PyHook, maybe Tkinter can catch keyevents the same way it does pyhook? It could be worth that I try to do it with tkinter instead of pyHook, then I'll just have to take care about the ImageGrab module if there is some error with modules.

Thanks for all your help!
May 28 '07 #18

bartonc
Expert 5K+
P: 6,596
Well, I've had some errors trying to run successfully the setup.py, some windows DLL and some quite difficult stuff for me. I'm so grateful for your help. I'm wondering that if the problem is with the module PyHook, maybe Tkinter can catch keyevents the same way it does pyhook? It could be worth that I try to do it with tkinter instead of pyHook, then I'll just have to take care about the ImageGrab module if there is some error with modules.

Thanks for all your help!
There is a screen grabber example in Tkinter, somewhere. I'll have a look around and see if I can find that for you. I used to use it back before I knew about the PtrScn button on my keyboard.
May 28 '07 #19

bartonc
Expert 5K+
P: 6,596
There is a screen grabber example in Tkinter, somewhere. I'll have a look around and see if I can find that for you. I used to use it back before I knew about the PtrScn button on my keyboard.
Nope, not Tkinter. It's win32. On my system the path is "D:\Python24\Lib\site-packages\win32\Demos" and the file is called "print_desktop.py". Good luck.
May 28 '07 #20

P: 10
Well I've been trying for long time with no succeed :( . So the only thing I can do now is ask if someone know how to get the "sstaker.exe" if he/she could create it and send it to me via email or something (I know it's a dangerous stuff but I don't know how to create that exe, hundreds of windows dll errors with no solucion).

Thanks again to all !
Jun 1 '07 #21

Post your reply

Sign in to post your reply or Sign up for a free account.