简体   繁体   English

如何将多个.py文件捆绑为一个.exe文件?

[英]How can I bundle multiple .py files into single .exe file?

[PROBLEM] [问题]

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

[CODE] Test example: I have a main.py + component.py. [CODE]测试示例:我有一个main.py + component.py。 [ main.py code ] [main.py代码]

 #   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. [component.py。 code ] 验证码]

#Import packages

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

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

[CURRENT RESULT] The exe file will check for component.py and because it will not find it, it will fail to open the msgBox. [当前结果]该exe文件将检查component.py,并且由于找不到该文件,因此无法打开msgBox。 Note: I have build only the main.py file Note: I am using python 2.7.10 + pyinstaller 3.0. 注意:我仅构建了main.py文件。注意:我正在使用python 2.7.10 + pyinstaller 3.0。

Consider using py2exe module. 考虑使用py2exe模块。

py2exe turns Python programs into packages that can be run on other Windows computers without needing to install Python on those computers. py2exe将Python程序转换为可以在其他Windows计算机上运行的程序包,而无需在这些计算机上安装Python。

Here it is link for the same. 这里是相同的链接。

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

Note: 注意:

Python 2.6, 2.7, 3.0, 3.1 Python 2.6、2.7、3.0、3.1

For Python 2.6, the DLL you need is called MSVCR90.dll. 对于Python 2.6,所需的DLL称为MSVCR90.dll。 Py2exe is not able to automatically include this DLL in your dist directory, so you must provide it yourself. Py2exe无法在您的dist目录中自动包含此DLL,因此您必须自己提供它。

To complicate things, there is more than one version of this DLL in existance, each with the same filename. 使事情复杂化的是,存在多个此DLL版本,每个版本具有相同的文件名。 You need the same version that the Python interpreter was compiled with, which is version 9.0.21022.8. 您需要使用与Python解释器相同的版本,即9.0.21022.8版本。 Through the remainder of these instructions, hover your mouse over the dll file (or the vcredist_x86.exe installer executable) to confirm which version you've got. 通过这些说明的其余部分,将鼠标悬停在dll文件(或vcredist_x86.exe安装程序可执行文件)上,以确认您拥有的版本。 You'll need the vcredist_x86.exe that contains the Microsoft Visual C++ 2008 Redistributable Package published 29-11-2007, so not the VS2008 SP1 one (tested with Python 2.7.1). 您需要vcredist_x86.exe,其中包含Microsoft Visual C ++ 2008可再发行组件包,于2007年11月29日发布,而不是VS2008 SP1(使用Python 2.7.1测试)。

As for older versions of Python, you need to check redist.txt within your Visual Studio installation to see whether you have the legal right to redistribute this DLL. 对于旧版本的Python,您需要在Visual Studio安装中检查redist.txt,以查看您是否具有重新分发此DLL的合法权利。 If you do have these rights, then you have the option to bundle the C runtime DLL with you application. 如果您确实拥有这些权利,那么您可以选择将C运行时DLL与您的应用程序捆绑在一起。 If you don't have the rights, then you must have your users run the redistributable C runtime installer on their machines. 如果您没有权限,则必须让用户在其计算机上运行可再发行的C运行时安装程序。

I came across this issue with python 3. I just used cx_freeze. 我在python 3中遇到了这个问题。我只是使用了cx_freeze。 It makes executables fairly well, and It works with setuptools. 它使可执行文件相当好,并且可以与setuptools一起使用。 It does leave you with a lot of extra files. 它确实给您留下了很多额外的文件。 To get around this I just developed a simple installer that bundles everything together and installs all of the files into "Program Files". 为了解决这个问题,我刚刚开发了一个简单的安装程序,将所有内容捆绑在一起,并将所有文件安装到“程序文件”中。

I used Inno Setup. 我使用了Inno Setup。 It is really easy to use. 真的很容易使用。 I recommend this approach. 我推荐这种方法。 Instead of spending hours to develop a single exe, bundle all of your files into a single exe installer which installs all of the files into the correct location. 无需花费数小时来开发单个exe,而是将所有文件捆绑到单个exe安装程序中,该安装程序会将所有文件安装到正确的位置。 http://www.jrsoftware.org/isinfo.php Inno Setup even has a setup wizard that does everything for you. http://www.jrsoftware.org/isinfo.php Inno Setup甚至具有一个安装向导,可以为您完成所有操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM