写了一个python程序(python2.7),用py2exe打包成一个native exe,结果老是报错
Traceback (most recent call last): File "p4_ticket_auth_jenkins.py", line 7, in <module> File "zipextimporter.pyc", line 82, in load_module File "jenkins\__init__.pyc", line 54, in <module> File "six.pyc", line 199, in load_module File "six.pyc", line 113, in _resolve File "six.pyc", line 80, in _import_moduleImportError: No module named httplib
乍一看,貌似是py2exe打包时没有把httplib打包进去。
File "p4_ticket_auth_jenkins.py", line 7是import jenkins,看来是jenkins的lib导入出的问题。于是把python程序的逻辑部分删掉
import sysimport argparseimport loggingimport jenkinsimport xml.etree.ElementTree as ETimport timeimport osfrom subprocess import Popen, PIPE, STDOUTimport subprocessimport commandsimport socketimport datetimedef main(): print("asdf") if __name__=="__main__": main()再用打包,运行打包后的exe,报错缺httplib。在python源码中import httplib,还报错缺urllib。import urllib,搞定
import sysimport argparseimport loggingimport jenkinsimport xml.etree.ElementTree as ETimport timeimport osfrom subprocess import Popen, PIPE, STDOUTimport subprocessimport commandsimport socketimport datetimeimport httplibimport urllib2def main(): print("asdf") if __name__=="__main__": main()
from distutils.core import setupimport py2exe, sys, ossys.argv.append('py2exe')setup( options = {'py2exe': {'bundle_files': 1, 'compressed': True}}, console = [{'script': "test.py"}], zipfile = None,)
这个setup.py可以把python代码(test.py)打包成native exe(不需要任何其他库的支持就能直接在windows上运行)。一条命令实现打包:
setup.py py2exe