如何将多个.py文件捆绑到单个.exe文件中?

时间:2015-10-26 12:12:29

标签: python pyinstaller

[问题]

input: multiple .py files
output: a single .exe file

[CODE] 测试示例: 我有一个main.py + component.py。 [main.py code]

 #   Import packages

import Tkinter
    from Tkinter import *
    import tkFileDialog
    import tkMessageBox

#Create dummy function

def showMessage():
import subprocess
execfile("component.py")
#Add button

root = Tkinter.Tk(className="test")
button= Button(root, text="Click", command=showMessage)
button.grid(row=10, column=3, sticky = W + E)
#Pack it

root.mainloop()

[component.py。代码]

#Import packages

import Tkinter
from Tkinter import *
import tkFileDialog
import tkMessageBox
#Create dummy function

def showMessage():
tkMessageBox.showinfo('test', 'test.')
showMessage()

[当前结果] exe文件将检查component.py,因为它找不到它,它将无法打开msgBox。 注意:我只构建了main.py文件 注意:我使用的是python 2.7.10 + pyinstaller 3.0。

2 个答案:

答案 0 :(得分:1)

考虑使用py2exe模块。

py2exe将Python程序转换为可以在其他Windows计算机上运行的软件包,而无需在这些计算机上安装Python。

这是相同的链接。

http://www.py2exe.org/index.cgi/Tutorial

注意:

Python 2.6,2.7,3.0,3.1

对于Python 2.6,您需要的DLL称为MSVCR90.dll。 Py2exe无法自动将此DLL包含在您的dist目录中,因此您必须自己提供它。

为了使事情复杂化,这个DLL存在多个版本,每个版本都有相同的文件名。您需要与编译Python解释器的版本相同的版本,即版本9.0.21022.8。通过这些说明的其余部分,将鼠标悬停在dll文件(或vcredist_x86.exe安装程序可执行文件)上以确认您已获得的版本。您需要包含Microsoft Visual C ++ 2008 Redistributable Package的vcredist_x86.exe,该版本包含29-11-2007,而不是VS2008 SP1(使用Python 2.7.1测试)。

对于旧版本的Python,您需要检查Visual Studio安装中的redist.txt,看看您是否拥有重新发布此DLL的合法权利。如果您拥有这些权限,则可以选择将C运行时DLL与您的应用程序捆绑在一起。如果您没有权限,则必须让您的用户在其计算机上运行可再发行的C运行时安装程序。

答案 1 :(得分:1)

我在python 3中遇到过这个问题。我刚用过cx_freeze。它使得可执行文件相当好,并且它与setuptools一起使用。它确实给你留下了很多额外的文件。为了解决这个问题,我刚开发了一个简单的安装程序,它将所有文件捆绑在一起并将所有文件安装到“Program Files”中。

我使用了Inno Setup。它真的很容易使用。我推荐这种方法。而不是花费数小时来开发单个exe,而是将所有文件捆绑到一个exe安装程序中,该安装程序将所有文件安装到正确的位置。 http://www.jrsoftware.org/isinfo.php Inno Setup甚至还有一个设置向导,可以为您完成所有操作。