py2exe error: "ImportError: No module named httplib"

写了一个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




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。