Skip to main content
 首页 » 编程设计

python之我如何将 .py 编译为 .exe

2025年05月04日11Free-Thinker

我正在尝试使用以下代码使用 py2exe 将我的 .py 脚本编译成 .exe:

from distutils.core import setup 
import py2exe, sys, os 
sys.argv.append('py2exe') 
setup( 
options = {'py2exe': {'optimize': 2}}, 
windows = [{'script': "get.py"}], 
zipfile = "shared.lib", 
) 

我在我的控制台中得到了这个:

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> ================================ RESTART ================================ 
>>>  
running py2exe 
*** searching for required modules *** 
*** parsing results *** 
creating python loader for extension 'unicodedata' (C:\Python27\DLLs\unicodedata.pyd -> unicodedata.pyd) 
creating python loader for extension 'select' (C:\Python27\DLLs\select.pyd -> select.pyd) 
creating python loader for extension '_hashlib' (C:\Python27\DLLs\_hashlib.pyd -> _hashlib.pyd) 
creating python loader for extension 'bz2' (C:\Python27\DLLs\bz2.pyd -> bz2.pyd) 
*** finding dlls needed *** 
*** create binaries *** 
*** byte compile python files *** 
writing byte-compilation script 'c:\docume~1\user\locals~1\temp\tmpduooti.py' 
C:\Python27\pythonw.exe -OO c:\docume~1\user\locals~1\temp\tmpduooti.py 
 
Traceback (most recent call last): 
  File "C:\Documents and Settings\User\Application Data\.minecraft\saves\HuFAdventure\setup.py", line 7, in <module> 
    zipfile = "shared.lib", 
  File "C:\Python27\lib\distutils\core.py", line 169, in setup 
    raise SystemExit, "error: " + str(msg) 
SystemExit: error: command 'C:\Python27\pythonw.exe' failed with exit status 1 
>>>  

你能帮忙吗

请您参考如下方法:

您是否有意构建基于图形用户界面的脚本?如果不将 windows = [{'script': "get.py"}], 更改为 console = ["get.py"],:

from distutils.core import setup 
import py2exe, sys, os 
sys.argv.append('py2exe') 
setup( 
    options = {'py2exe': {'optimize': 2}}, 
    console = ["get.py"], 
    zipfile = "shared.lib", 
) 

如果你正在构建一个窗口程序,那么忽略它并留下评论,这样我就可以删除这个答案。