gpt4 book ai didi

python - py2exe 和 pyinstaller 生成的 exe 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 13:59:55 25 4
gpt4 key购买 nike

我使用python编写了一个屏幕截图程序,并想将其编译为.exe文件。所以我尝试了 py2exepyinstaller

我的Python版本是2.7.14,32位。我使用Windows 10。我在这个项目中也使用了虚拟环境。

我的屏幕截图程序代码如下。我通过 python snapshot.py 运行它,它会截取我的屏幕截图并将其存储在保存目录中。

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

py安装程序

对于pyinstaller,我只需使用pip install pyinstaller来安装它。已安装的版本 -> 3.3.1。请注意,我在虚拟环境中安装了这个包。

我通过运行pyinstaller --onefile snapshot.py编译了该程序。它生成了一个可执行的screenshot.exe。当我运行可执行文件时,没有截图。

py2exe

对于安装py2exe,由于在运行python 2的Windows计算机上安装它存在一些问题,我按照此link中的教程进行操作。

我创建setup.py来将screenshot.py编译为screenshot.exe。以下是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
)

我使用python setup.py py2exe运行它。它生成了一个可执行文件。当我运行这个文件时,结果与 pyinstaller 相同。没有截图。

我需要有关为什么 screenshot.exe 无法工作的帮助。我错过了什么吗?

感谢您的帮助。

最佳答案

解决了!

下面是screenshot.py的修改后的代码。通过 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()

事实证明,我需要包含 multiprocessing 中的 freeze_supportProcess

关于python - py2exe 和 pyinstaller 生成的 exe 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49326762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com