由 py2exe 和 pyinstaller 生成的 exe 不起作用 [英] exe generated by py2exe and pyinstaller not working
问题描述
我用python写了一个截图程序,想把它编译成.exe
文件.所以我尝试了 py2exe
和 pyinstaller
.
I wrote a screenshot program using python and wanted to compile it as .exe
file. So I tried with both py2exe
and pyinstaller
.
我的 python 版本是 2.7.14
,32 位.我使用 Windows 10.我也在这个项目中使用虚拟环境.
My python version is 2.7.14
, 32bit. I use Windows 10. I also use virtual environment in this project.
我的截图程序代码如下.我通过 python screenshot.py
运行它,它截取我的屏幕截图并将其存储在保存目录中.
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
对于pyinstaller
,我只需使用pip install pyinstaller
安装它.安装的版本 -> 3.3.1
.请注意,我在虚拟环境中安装了这个包.
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.
我通过运行 pyinstaller --onefile screenshot.py
来编译程序.它生成了一个可执行的screenshot.exe
.当我运行可执行文件时,没有截图.
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
,由于在运行 python 2 的 Windows 计算机上安装它存在一些问题,我遵循了这个 link
For installing py2exe
, since there is some issue regarding installing it on Windows computer running python 2, I followed the tutorial from this link
我创建了 setup.py
以将 screenshot.py
编译为 screenshot.exe
.下面是 setup.py
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
)
我使用 python setup.py py2exe
运行它.它生成了一个可执行文件.当我运行此文件时,结果与 pyinstaller
相同.没有截图.
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.
我需要关于为什么 screenshot.exe
不起作用的帮助.我错过了什么吗?
I need help on why does the screenshot.exe
not working. Am I missing something?
感谢您的帮助.
推荐答案
解决了!
以下是screenshot.py
的修改后的代码.通过 py2exe
运行它.
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()
事实证明,我需要从 multiprocessing
中包含 freeze_support
和 Process
It turned out that I need to include freeze_support
and Process
from multiprocessing
这篇关于由 py2exe 和 pyinstaller 生成的 exe 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!