写了一个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_module ImportError: No module named httplib
乍一看,貌似是py2exe打包时没有把httplib打包进去。
File "p4_ticket_auth_jenkins.py", line 7是import jenkins,看来是jenkins的lib导入出的问题。于是把python程序的逻辑部分删掉
import sys import argparse import logging import jenkins import xml.etree.ElementTree as ET import time import os from subprocess import Popen, PIPE, STDOUT import subprocess import commands import socket import datetime def main(): print("asdf") if __name__=="__main__": main()
再用打包,运行打包后的exe,报错缺httplib。在python源码中import httplib,还报错缺urllib。import urllib,搞定
import sys import argparse import logging import jenkins import xml.etree.ElementTree as ET import time import os from subprocess import Popen, PIPE, STDOUT import subprocess import commands import socket import datetime import httplib import urllib2 def main(): print("asdf") if __name__=="__main__": main()
附上setup.py的代码:
from distutils.core import setup import py2exe, sys, os sys.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
时间: 2024-10-09 00:49:33