我一直在尝试使用PyInstaller将基于wxPython的python脚本转换为.exe文件,但我还没有成功。转换后会创建一个.exe文件,但会显示错误消息"无法执行脚本主文件"一旦点击.exe文件就弹出我们。
我的代码如下:
import wx
import sys
class MyAppWindow(wx.Frame):
def __init__(self, title='Path Finder'):
'''
Constructor
'''
wx.Frame.__init__(self, None, 111, title, size=(640,480))
filemenu = wx.Menu()
filemenu.Append(1,"&About","Create a new file")
filemenu.AppendSeparator()
filemenu.Append(2,"&Quit","Quit the Application")
#Creating File menu
viewmenu = wx.Menu()
viewmenu.Append(3,"&Shrink","Reduce size to half")
viewmenu.Append(4,"&Maximize","Maximize window")
viewmenu.Append(5,"&Full screen","Switch to full screen mode")
viewmenu.Append(6,"&Reset","Reset View")
#Creating view menu
menubar = wx.MenuBar()
menubar.Append(filemenu,"&File")
menubar.Append(viewmenu,"&View")
self.SetMenuBar(menubar)
#Fitting File and edit in menu bar
panel2 = wx.TextCtrl(self, 7, style=wx.TE_MULTILINE|wx.TE_READONLY)
panel2.SetValue("Hello World! \nI am trying to make .exe files using Python!")
# Created Panels
vbox1 = wx.BoxSizer(wx.VERTICAL)
#vbox1.Add(panel1, 1, wx.EXPAND)
vbox1.Add(panel2, 2, wx.EXPAND)
#vbox1.Add(panel3,1,wx.EXPAND)
self.SetSizer(vbox1)
#Sizr to adjust panel
wx.EVT_MENU(self, 2, self.onQuit)
wx.EVT_MENU(self, 3, self.onViewShrink)
wx.EVT_MENU(self, 4, self.onViewMaximize)
wx.EVT_MENU(self, 5, self.onViewFullScreen)
wx.EVT_MENU(self, 6, self.onViewReset)
wx.EVT_MENU(self, 1, self.OnAbout)
#Associating the menu buttons with their respective functions
self.Show()
def onQuit(self, e):
print("Good bye!")
self.Close()
#Quits the GUI
def onViewReset(self,e):
self.ShowFullScreen(False)
self.SetSize((640,480))
self.Center()
#Restes the GUI to center
def onViewFullScreen(self,e):
self.ShowFullScreen(True)
#Full Screen
def onViewMaximize(self,e):
self.Maximize(True)
#Maximises the GUI
def onViewShrink(self,e):
msize = []
for i in self.GetSize():
msize.append(int(0.75*i))
self.SetSize(msize)
#Shrinks the GUI
def OnAbout(self, e):
d = wx.MessageDialog(self, "Program to learn Python to exe conversion\n"
,"Python to exe using PyInstaller", wx.OK)
d.ShowModal() # show the dialog
d.Destroy()
#About menu
#----------------------------------------------------------------------
myApp = wx.App()
myFrame = MyAppWindow()
myApp.MainLoop()
#End of Python script
我在命令提示符下输入的命令如下: pyinstaller.exe --onefile --windowed MyAppWindow.py
命令提示符中显示的消息如下:
829 INFO: PyInstaller: 3.3.1
830 INFO: Python: 2.7.11
833 INFO: Platform: Windows-10-10.0.16299
838 INFO: wrote C:\Users\Smit\workspace\Amobeo_GUI2exe\src\MyAppWindow.spec
845 INFO: UPX is not available.
856 INFO: Extending PYTHONPATH with paths
['C:\\Users\\Smit\\workspace\\Amobeo_GUI2exe\\src',
'C:\\Users\\Smit\\workspace\\Amobeo_GUI2exe\\src']
857 INFO: checking Analysis
871 INFO: Building Analysis because out00-Analysis.toc is non existent
873 INFO: Initializing module dependency graph...
892 INFO: Initializing module graph hooks...
1178 INFO: running Analysis out00-Analysis.toc
1191 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
required by c:\users\smit\appdata\local\enthought\canopy\system\scripts\python.exe
1549 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4967_none_acd2fb3be1d90a9f.manifest
1554 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_acd388d7e1d8689f.manifest
1560 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9317_none_acce041fe1dd7bc9.manifest
1937 INFO: Searching for assembly amd64_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.9317_none ...
1942 INFO: Found manifest C:\WINDOWS\WinSxS\Manifests\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9317_none_08e0939fa840d57b.manifest
2012 INFO: Searching for file msvcr90.dll
2015 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9317_none_08e0939fa840d57b\msvcr90.dll
2035 INFO: Searching for file msvcp90.dll
2098 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9317_none_08e0939fa840d57b\msvcp90.dll
2128 INFO: Searching for file msvcm90.dll
2159 INFO: Found file C:\WINDOWS\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9317_none_08e0939fa840d57b\msvcm90.dll
2560 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4967_none_acd2fb3be1d90a9f.manifest
2570 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.6161_none_acd388d7e1d8689f.manifest
2580 INFO: Found C:\WINDOWS\WinSxS\Manifests\amd64_policy.9.0.microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.9317_none_acce041fe1dd7bc9.manifest
2592 INFO: Adding redirect Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 9317)
3097 INFO: Caching module hooks...
3129 INFO: Analyzing C:\Users\Smit\workspace\Amobeo_GUI2exe\src\MyAppWindow.py
3175 INFO: Loading module hooks...
3179 INFO: Loading module hook "hook-encodings.py"...
11202 INFO: Looking for ctypes DLLs
11204 INFO: Analyzing run-time hooks ...
11225 INFO: Looking for dynamic libraries
11417 INFO: Looking for eggs
11418 INFO: Using Python library c:\users\smit\appdata\local\enthought\canopy\system\scripts\python27.dll
11420 INFO: Found binding redirects:
[BindingRedirect(name=u'Microsoft.VC90.CRT', language=None, arch=u'amd64', oldVersion=(9, 0, 21022, 8), newVersion=(9, 0, 30729, 9317), publicKeyToken=u'1fc8b3b9a1e18e3b')]
11437 INFO: Warnings written to C:\Users\Smit\workspace\Amobeo_GUI2exe\src\build\MyAppWindow\warnMyAppWindow.txt
11543 INFO: Graph cross-reference written to C:\Users\Smit\workspace\Amobeo_GUI2exe\src\build\MyAppWindow\xref-MyAppWindow.html
11834 INFO: checking PYZ
11836 INFO: Building PYZ because out00-PYZ.toc is non existent
11838 INFO: Building PYZ (ZlibArchive) C:\Users\Smit\workspace\Amobeo_GUI2exe\src\build\MyAppWindow\out00-PYZ.pyz
12781 INFO: Building PYZ (ZlibArchive) C:\Users\Smit\workspace\Amobeo_GUI2exe\src\build\MyAppWindow\out00-PYZ.pyz completed successfully.
13014 INFO: checking PKG
13016 INFO: Building PKG because out00-PKG.toc is non existent
13018 INFO: Building PKG (CArchive) out00-PKG.pkg
13146 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 9317)
18030 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
18073 INFO: Bootloader c:\users\smit\appdata\local\enthought\canopy\system\lib\site-packages\PyInstaller\bootloader\Windows-64bit\runw.exe
18076 INFO: checking EXE
18079 INFO: Building EXE because out00-EXE.toc is non existent
18081 INFO: Building EXE from out00-EXE.toc
18084 INFO: Appending archive to EXE C:\Users\Smit\workspace\Amobeo_GUI2exe\src\dist\MyAppWindow.exe
19056 INFO: Building EXE from out00-EXE.toc completed successfully.
(图片:Message displayed in command prompt)
上述过程在我的源文件夹中创建了两个文件夹,即' build'和' dist'名为MyAppWindow.exe的exe文件是在' dist'内创建的。夹。但是,单击它时会弹出以下错误消息:
致命错误! "无法执行MyAppWindow" (图片:Error message)
当我运行Python脚本时,我的GUI看起来像这样: GUI created by the Python script
我在Windows上使用Python 2.7.11 - 64位
我很乐意为你提供帮助。如果您有任何问题,请与我们联系。
答案 0 :(得分:0)
在Windows 10 Pro 64bit上测试:
下载并安装Winpython Zero 2.7.13 64位(Link,非常方便,可移植,通过单击/执行" WinPython命令Prompt.exe"激活,没有搞乱使用virtualenv,不会触及你的系统Python,只是在不再需要时删除文件夹)
pip install wxPython
,安装最新的wx
pip install https://github.com/pyinstaller/pyinstaller/tarball/develop
--clean
会很有帮助): pyinstaller --clean --windowed --onefile app.py
该应用看起来就像在你的屏幕截图中一样。
我首先收到了与您相同的错误消息。查看警告文件(11437 INFO: Warnings written to …
),结果发现我忘了pip install wxPython。