Windows下用py2exe将Python程序打包成exe程序的教程

yipeiwu_com5年前Python基础

py2exe在sourceforge 的下载只支持到2.7。

针对python3.0+的版本,需要自己编译。
1.下载源码

svn checkout svn://svn.code.sf.net/p/py2exe/svn/trunk py2exe-svn
2.编译环境

这里使用的是vs2014.
3.安装

进入py2exe-3

python setup.py install

这里会进行编译、安装。

此外,python默认使用的是vs9,针对vs2014,需要改下文件:

复制代码 代码如下:
Lib\distutils\msvc9compiler.py

寻找:

复制代码 代码如下:
VERSION = get_build_version()

在下面增加:

复制代码 代码如下:
VERSION = 11.0

如果出现错误:

复制代码 代码如下:
Failed to load and parse the manifest. The system cannot find the file specified.

error: command 'mt.exe' failed with exit status 31

解决办法:由于vs2010后的link.exe的参数稍微有些改变,所以在link的时候没有生成manifest文件,自然mt.exe找不到这个文件。只需要在msvc9compiler.py里面搜索一下MANIFESTFILE,然后在他上面加一行 ld_args.append('/MANIFEST'),保存就OK了。(python3.4好像没有这个问题,2.7存在)
4.setup.py

setup.py可以参考官网,其中的参数--bundle-files,需要特别说下,想打成一个整包要设成0.

变化可以参考:http://sourceforge.net/p/py2exe/svn/HEAD/tree/trunk/py2exe-3/
最后附上setup.py

from distutils.core import setup
import py2exe
import sys,os
 
if sys.version_info.major >= 3.0:
  opt_bundle_files = 0
else:
  opt_bundle_files = 1
includes = ["PyQt4.QtCore","PyQt4.QtGui","sip"]
options = {"py2exe":
     { "compressed": 1,
      "optimize": 2,
      "includes": includes,
      "bundle_files": opt_bundle_files,
     }
   }
setup(
  version = "0.1.0",
  description = "test_add",
  options = options,
  zipfile=None,
  console=[{"script": "test_add.py", "icon_resources": [(1, "py.ico")] }],
  #windows=[{"script": "test_add.py", "icon_resources": [(1, "py.ico")] }],
)

相关文章

python 实现将多条曲线画在一幅图上的方法

python 实现将多条曲线画在一幅图上的方法

如下所示: # -*- coding: utf-8 -*- """ Created on Thu Jun 07 09:17:40 2018 @author: yjp """ imp...

跟老齐学Python之通过Python连接数据库

用Python来编写网站,必须要能够通过python操作数据库,所谓操作数据库,就是通过python实现对数据的连接,以及对记录、字段的各种操作。上一讲提到的那种操作方式,是看官直接通过...

wxpython多线程防假死与线程间传递消息实例详解

wxpython多线程防假死与线程间传递消息实例详解

wxpython中启用线程的方法,将GUI和功能的执行分开。 网上关于python多线程防假死与线程传递消息是几年前的,这里由于wxpython和threading模块已经更新最新,因此...

深入解析神经网络从原理到实现

深入解析神经网络从原理到实现

1.简单介绍 在机器学习和认知科学领域,人工神经网络(artificial neural network,缩写ANN),简称神经网络(neural network,缩写NN)或类神经网...

python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法

Python的字符集处理实在蛋疼,目前使用UTF-8居多,然后默认使用的字符集是ascii,所以我们需要改成utf-8 查看目前系统字符集 复制代码 代码如下: import sys p...