py2exe 可以用来将python program 以exe的形式发布。
安装py2exe
对于python 3.x
pip install py2exe
可以直接安装
对于python 2.7, 需要下载exe安装,
https://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/
这里详细记录一下python2.7下 py2exe的安装注意的问题
1. 安装时出现如下错误,
Python version 2.7 required, which was not found in the registry
解决:
copy下面的脚本,创建一个registry.py, 然后运行python registry.py
import sys
from _winreg import *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\Python\Pythoncore\%s\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\Lib\;%s\DLLs\" % (
installpath, installpath, installpath
)
def RegisterPy():
try:
reg = OpenKey(HKEY_CURRENT_USER, regpath)
except EnvironmentError as e:
try:
reg = CreateKey(HKEY_CURRENT_USER, regpath)
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
except:
print "*** Unable to register!"
return
print "--- Python", version, "is now registered!"
return
if (QueryValue(reg, installkey) == installpath and
QueryValue(reg, pythonkey) == pythonpath):
CloseKey(reg)
print "=== Python", version, "is already registered!"
return
CloseKey(reg)
print "*** Unable to register!"
print "*** You probably have another Python installation!"
if __name__ == "__main__":
RegisterPy()
- 错误
import py2exe_util
ImportError: DLL load failed: %1 is not a valid Win32 application.
解决: 这是安装错了py2exe的版本。 下载列表中的win64, amd64似乎不是指OS的版本。我PC是win64,但是安装win64版本有上述错误。安装py2exe-0.6.9.win32-py2.7.exe后解决了问题。
生成exe
按照http://www.py2exe.org/index.cgi/Tutorial#Step1中的guide 不再详述。
注意的问题:
如果生成的exe遇到local module 找不到的情况,
解决方案: 在所有subfolder中加上空白的 __init__.py