Skip to content

Instantly share code, notes, and snippets.

@shellexy
Created January 4, 2019 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shellexy/7ce8a944c6995f542fd487b5aac47c68 to your computer and use it in GitHub Desktop.
Save shellexy/7ce8a944c6995f542fd487b5aac47c68 to your computer and use it in GitHub Desktop.
在 linux 下打包 python 程序为 exe
#!/bin/bash -x
##
## 在 linux 下打包 python 程序为 exe
##
## @Note: 如果不希望出现 cmd 窗口的话,可以加参数 --base-name=win32gui
##
## wine pygtk 包可以从 http://ftp.gnome.org/pub/GNOME/binaries/win32/pygtk/ 下载 pygtk-all-in-one 的 .msi
##
## 安装后可以删掉 $WINEPREFIX/drive_c/Python27/Lib/site-packages/gtk-2.0/runtime 下
## 多余的 locale icons 以及 .a .lib .def man 文件以减少体积
##
unset PYTHONSTARTUP
export WINEPREFIX="$HOME/.wineapps/python2.7"
wine 'C:\Python27\python.exe' 'C:\Python27\Scripts\cxfreeze' "$@" || exit
set +x
## 如果使用 --target-dir 参数指定了非 dist 目录
## 解析 --target-dir 参数
parseopt() {
while [ -n "$1" ]; do
case "$1" in
--target-dir|--install-dir) DIST=$2 ; shift ;;
--target-dir=*|--install-dir=*) DIST=${1#*=} ; shift ;;
-h|--help) exit ;;
--) shift ; break ;;
*) shift ;;
esac
done
}
parseopt "$@"
DIST="${DIST:-dist}"
## 因为 wine 下不会拷贝 python27.dll
test -d "$DIST" && cp -uv "$WINEPREFIX/drive_c/windows/system32/python27.dll" "$DIST"
## 因为有些机器缺少 msvcr90.dll,会报错“由于应用程序配置不正确……”而无法运行
test -d "$DIST" && cp -uv "$WINEPREFIX"/drive_c/windows/winsxs/*ww*/msvcr90.dll "$DIST"
test -d "$DIST" && echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<noInheritable></noInheritable>
<assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
<file name="msvcr90.dll" />
</assembly>' > "$DIST/Microsoft.VC90.CRT.manifest"
## 针对 pygtk 程序
GTKDIR="$WINEPREFIX/drive_c/Python27/Lib/site-packages/gtk-2.0/runtime"
test -a "$DIST/gtk._gtk.pyd" &&
cp -au "$GTKDIR/etc" "$DIST" &&
cp -au "$GTKDIR/lib" "$DIST" &&
cp -au "$GTKDIR/share" "$DIST" &&
cp -au "$GTKDIR/bin/"*dll "$DIST"
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment