将Python编译为exe

时间:2013-07-19 17:31:23

标签: python-2.7 py2exe distutils pyinstaller

在上周,我一直在努力使用pyinstaller / py2exe将一些python脚本和pyqt .ui文件编译成exe。我对编程很新,并且从未试图在之前打包分发应用程序。

到目前为止,我已经成功地将我的gui应用程序简化为一个python脚本,但是当我尝试使用py2exe或pyinstall编译它时,我得到了同样的错误。

使用pyinstaller时,错误为:

C:\Python27\pyinstaller-2.0>python utils\build.py costaharmplotter.spec
109 INFO: Testing for ability to set icons, version resources...
187 INFO: ... resource update available
203 INFO: UPX is not available.
2515 WARNING: library python%s%s required via ctypes not found
2625 INFO: checking Analysis
2625 INFO: building Analysis because out00-Analysis.toc non existent
2625 INFO: running Analysis out00-Analysis.toc
2625 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable

2640 INFO: Searching for assembly x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21
022.8_x-ww ...
2640 INFO: Found manifest C:\WINDOWS\WinSxS\Manifests\x86_Microsoft.VC90.CRT_1fc
8b3b9a1e18e3b_9.0.21022.8_x-ww_d08d0375.manifest
2656 INFO: Searching for file msvcr90.dll
2656 INFO: Found file C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_
9.0.21022.8_x-ww_d08d0375\msvcr90.dll
2656 INFO: Searching for file msvcp90.dll
2656 INFO: Found file C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_
9.0.21022.8_x-ww_d08d0375\msvcp90.dll
2656 INFO: Searching for file msvcm90.dll
2656 INFO: Found file C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_
9.0.21022.8_x-ww_d08d0375\msvcm90.dll
2812 INFO: Analyzing C:\Python27\pyinstaller-2.0\support\_pyi_bootstrap.py
5484 WARNING: library python%s%s required via ctypes not found
5562 INFO: Analyzing C:\Python27\pyinstaller-2.0\PyInstaller\loader\archive.py
5875 INFO: Analyzing C:\Python27\pyinstaller-2.0\PyInstaller\loader\carchive.py
6297 WARNING: pywintypes is changing its name to pywintypes27
6797 INFO: Analyzing C:\Python27\pyinstaller-2.0\PyInstaller\loader\iu.py
6843 INFO: Analyzing costaharmplotter.py
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1844, in Tcl
    return Tk(screenName, baseName, className, useTk)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1745, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, want
objects, useTk, sync, use)
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
    {C:\Tcl\lib\tcl8.5} C:/Python27/lib/tcl8.5 C:/lib/tcl8.5 C:/lib/tcl8.5 C:/li
brary C:/library C:/tcl8.5.2/library C:/tcl8.5.2/library

C:/Tcl/lib/tcl8.5/init.tcl: version conflict for package "Tcl": have 8.5.2, need
 exactly 8.5.13
version conflict for package "Tcl": have 8.5.2, need exactly 8.5.13
    while executing
"package require -exact Tcl 8.5.13"
    (file "C:/Tcl/lib/tcl8.5/init.tcl" line 18)
    invoked from within
"source C:/Tcl/lib/tcl8.5/init.tcl"
    ("uplevel" body line 1)
    invoked from within
"uplevel #0 [list source $tclfile]"


This probably means that Tcl wasn't installed properly.

我在使用py2exe时遇到了类似的错误:

C:\Documents and Settings\T53983\Desktop\HarmonicPlotterGUI>python setup.py py2e
xe
running py2exe
*** searching for required modules ***
*** parsing results ***
Traceback (most recent call last):
  File "setup.py", line 4, in <module>
    setup(data_files=['C:\\Python27\\tcl\\tcl8.5\\init.tcl'], console = ['costah
armplotter.py'])
  File "C:\Python27\lib\distutils\core.py", line 152, in setup
    dist.run_commands()
  File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 243, in run
    self._run()
  File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 299, in _run
    py_files, extensions, builtins = self.parse_mf_results(mf)
  File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 1097, in parse_
mf_results
    tk = _tkinter.create()
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
    {C:\Tcl\lib\tcl8.5} C:/Python27/lib/tcl8.5 C:/lib/tcl8.5 C:/lib/tcl8.5 C:/li
brary C:/library C:/tcl8.5.2/library C:/tcl8.5.2/library

C:/Tcl/lib/tcl8.5/init.tcl: version conflict for package "Tcl": have 8.5.2, need
 exactly 8.5.13
version conflict for package "Tcl": have 8.5.2, need exactly 8.5.13
    while executing
"package require -exact Tcl 8.5.13"
    (file "C:/Tcl/lib/tcl8.5/init.tcl" line 18)
    invoked from within
"source C:/Tcl/lib/tcl8.5/init.tcl"
    ("uplevel" body line 1)
    invoked from within
"uplevel #0 [list source $tclfile]"


This probably means that Tcl wasn't installed properly.

在我的脚本中,我使用numpy,pyqtgraph.PlotWidget,pyqt4.QtCore和pyqt4.qtgui。

  • 如果我没有使用tkinter,为什么py2exe / pyinstaller正在寻找tcl?
  • 如何排除导致此问题的此程序包?我已经将tcl文件从tcl 8.5.13切换到tcl 8.5.2,但它仍然不起作用。

请不要将我链接到py2exe / pyinstaller文档,我过去几天一直在阅读它,我仍在努力了解所有选项的工作原理。我将在下面发布我的setup.py文件和spec文件以供参考。

setup.py文件:

from distutils.core import setup
import py2exe

setup(console = ['costaharmplotter.py'])

规范文件:

# -*- mode: python -*-
a = Analysis(['costaharmplotter.py'],
             pathex=['C:\\Python27'],
             hiddenimports=[],
             hookspath=None)
pyz = PYZ(a.pure - [('Tkinter','C:\\Python27\\lib\\lib-tk','PYMODULE')])
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=1,
          name=os.path.join('build\\pyi.win32\\costaharmplotter', 'costaharmplotter.exe'),
          debug=False,
          strip=None,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=None,
               upx=True,
               name=os.path.join('dist', 'costaharmplotter'))

1 个答案:

答案 0 :(得分:0)

上次我尝试使用py2exe时,我无法使用比Python 2.6更新的东西。查看Sourceforge和旧版update/news page,看起来仍然不支持2.6 / 2.7以外的任何内容。

看起来你正在使用2.7,所以请尝试使用Python 2.6进行编译。