如何将Python 3应用编译为.exe? [英] How do I compile my Python 3 app to an .exe?

查看:141
本文介绍了如何将Python 3应用编译为.exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将我的Python应用程序转换为 .exe ?我用 tkinter 编写了一个程序,想知道如何让其他人使用它。我使用Python 3.3。我进行了一些搜索,但找不到任何内容。

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.

推荐答案

cx_Freeze可以做到这一点,但是创建了一个包含很多依赖项的文件夹。 py2exe 现在可以执行此操作,并使用--bundle-files 0选项创建一个EXE ,这可能是您问题的最佳解决方案。

cx_Freeze does this but creates a folder with lots of dependencies. py2exe now does this and, with the --bundle-files 0 option, creates just one EXE, which is probably the best solution to your question.

更新:在遇到py2exe无法查找的第三方模块后,我已经以kotlet的身份移至pyinstaller schabowy建议如下。两者都有足够的文档,并且包含可以通过命令行参数运行的.exe文件,但是我还没有编译pyinstaller无法在不进行调试或从头开始的情况下无法处理的脚本。

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.

这是一个简单的便捷函数,我用解释器的默认值构建了一个.exe(当然也可以使用批处理或类似方法):

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

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)

这篇关于如何将Python 3应用编译为.exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆