如何将我的 Python 3 应用程序编译为 .exe?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17907258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I compile my Python 3 app to an .exe?
提问by Samir Chahine
How do I convert my Python app to a .exe
? I made a program with tkinter
and was wondering how to make it possible for others to use. I use Python 3.3. I searched for a bit but could not find anything.
如何将我的 Python 应用程序转换为.exe
? 我制作了一个程序,tkinter
并想知道如何让其他人使用它。我使用 Python 3.3。我搜索了一下,但找不到任何东西。
采纳答案by gss
cx_Freeze does this but creates a folder with lots of dependencies. py2exenow does this and, with the --bundle-files 0 option, creates just one EXE, which is probably the best solution to your question.
cx_Freeze 会这样做,但会创建一个包含大量依赖项的文件夹。py2exe现在执行此操作,并且使用 --bundle-files 0 选项仅创建一个 EXE,这可能是您问题的最佳解决方案。
UPDATE: After encountering third-party modules that py2exe had trouble "finding", I've moved to pyinstaller as kotlet schabowy suggests below. Both have ample documentation and include .exes you can run with command line parameters, but I have yet to compile a script that pyinstaller isn't able to handle without debugging or head-scratching.
更新:在遇到 py2exe 无法“找到”的第三方模块后,我已按照下面的 kotlet schabowy 建议转移到 pyinstaller。两者都有丰富的文档,并包含您可以使用命令行参数运行的 .exe,但我还没有编译一个脚本,pyinstaller 在没有调试或头疼的情况下无法处理。
Here's a simple convenience function I use to build an .exe with my defaults from the interpreter (of course a batch or similar would be fine too):
这是我用来使用解释器中的默认值构建 .exe 的一个简单的便利函数(当然批处理或类似的也可以):
import subprocess,os
def exe(pyfile,dest="",creator=r"C:\Python34\Scripts\pyinstaller.exe",ico=r"C:\my icons\favicon.ico",noconsole=False):
insert=""
if dest: insert+='--distpath ""'.format(dest)
else: insert+='--distpath "" '.format(os.path.split(pyfile)[0])
if ico: insert+=' --icon="{}" '.format(ico)
if noconsole: insert+=' --noconsole '
runstring='"{creator}" "{pyfile}" {insert} -F'.format(**locals())
subprocess.check_output(runstring)
回答by kotlet schabowy
I have found PyInstallerto work the best. You have many options for example you can pack everything to a one file exe.
我发现PyInstaller效果最好。您有很多选择,例如您可以将所有内容打包到一个文件 exe 中。
I love to use it together with Cythonfor speed.
我喜欢将它与Cython一起使用以提高速度。
回答by kotlet schabowy
Use Pyinstaller. After installing it, open terminal in the directory where your project resides.
使用Pyinstaller。安装完成后,在你的项目所在目录中打开终端。
$ pyinstaller script1.py script2.py ...
(where script1, script2, etc. are all the scripts used in your project.)After command is completed, open
dist
folder and enter the subdirectory. There you'll find an executable.
$ pyinstaller script1.py script2.py ...
(其中 script1、script2 等是您项目中使用的所有脚本。)命令完成后,打开
dist
文件夹,进入子目录。在那里你会找到一个可执行文件。
Hope it helps.
希望能帮助到你。