exe generated by py2exe and pyinstaller not working

故事扮演 提交于 2020-01-15 09:16:55

问题


I wrote a screenshot program using python and wanted to compile it as .exe file. So I tried with both py2exe and pyinstaller.

My python version is 2.7.14, 32bit. I use Windows 10. I also use virtual environment in this project.

My code for the screenshot program is like below. I run it by python screenshot.py and it take a screenshot of my screen and stored it inside the save directory.

from PIL import Image
import pyscreenshot as ImageGrab
import time


time.sleep(3)


save_dir = "C:/Users/ling/Downloads/test/"

def grab():
    im = ImageGrab.grab()
    im.save(save_dir + "screenshot.png")


if __name__ == "__main__":
    grab()

pyinstaller

for pyinstaller, I simply install it using pip install pyinstaller. The version that was installed -> 3.3.1. Note that I install this package inside virtual environment.

I compiled the program by running pyinstaller --onefile screenshot.py. It generated an executable screenshot.exe. When I run the executable, no screenshot was taken.

py2exe

For installing py2exe, since there is some issue regarding installing it on Windows computer running python 2, I followed the tutorial from this link

I create setup.py to compiled screenshot.py as screenshot.exe. Below is the code for setup.py

from distutils.core import setup
import py2exe

setup(
      console=[{'script':'screenshot.py'}],
      options = {
            'py2exe': {
                'includes': ['PIL','pyscreenshot','time'],
                'bundle_files': 1, 'compressed': True
             }
      },
      zipfile = None
)

I run it by using python setup.py py2exe. It generated a single executable file. When I run this file, the result is same as pyinstaller. There is no screenshot taken.

I need help on why does the screenshot.exe not working. Am I missing something?

Thank you for your help.


回答1:


Solved it!

Below are the modified code for screenshot.py. Run it via py2exe.

from multiprocessing import Process, freeze_support
from PIL import Image
import pyscreenshot as ImageGrab
import time

time.sleep(3)


save_dir = "C:/Users/ling/Downloads/test/"

def grab():
    im = ImageGrab.grab()
    im.save(save_dir + "screenshot.png")

if __name__ == "__main__":
    freeze_support()
    p = Process(target=grab)
    p.start()

It turned out that I need to include freeze_support and Process from multiprocessing



来源:https://stackoverflow.com/questions/49326762/exe-generated-by-py2exe-and-pyinstaller-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!