Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python py2exe和pyinstaller生成的exe不工作_Python_Pyinstaller_Py2exe - Fatal编程技术网

Python py2exe和pyinstaller生成的exe不工作

Python py2exe和pyinstaller生成的exe不工作,python,pyinstaller,py2exe,Python,Pyinstaller,Py2exe,我使用python编写了一个屏幕截图程序,并希望将其编译为.exe文件。所以我尝试了py2exe和pyinstaller 我的python版本是2.7.14,32位。我使用Windows10。我也在这个项目中使用虚拟环境 我的截图程序代码如下所示。我通过python screenshot.py运行它,它获取我的屏幕截图并将其存储在save目录中 from PIL import Image import pyscreenshot as ImageGrab import time time.sl

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

我的python版本是
2.7.14
,32位。我使用Windows10。我也在这个项目中使用虚拟环境

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

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 对于
pyinstaller
,我只需使用
pip install pyinstaller
安装它。已安装的版本->
3.3.1
。请注意,我是在虚拟环境中安装此软件包的

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

py2exe 对于安装
py2exe
,由于在运行python 2的Windows计算机上安装它时存在一些问题,因此我遵循了本教程

我创建
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()

事实证明,我需要包括
多处理

中的
冻结\u支持
过程
,当您运行代码时,它可以工作?是的。当我使用
python screenshot.py
运行代码时,它可以工作。我不知道从
screenshot.exe执行是否需要较长时间是问题所在,还是我遗漏了什么。如文档
grab\u to\u文件中所述:将屏幕内容复制到文件中。内部功能!使用PIL.Image.save()将图像保存到文件。
。使用来自PIL的图像。另外,有些库py2exe无法直接支持,我很少使用它,所以我无法说出发生了什么,但我在这里和这里发现了一些有用的东西