将发送邮件的python代码用py2exe编译成exe文件遇到问题的解决方法

将发送邮件的python代码用py2exe编译成exe文件遇到问题的解决方法python

问题描述:api

   如下代码为发送邮件的python源码,源码执行ok,但当用py2exe编译成exe文件后,执行,报错以下:

解决方法:

注意:要在dos命令行下执行生成后的exe文件,这样会显示报错信息!!
问题延伸:
一些模块如email的__init__.py 为了确保和一些早期版本的兼容,它不可以自动处理包加载,所以须要手动加载。
如下模块可能会遇到相同的问题:
['FCNTL', 'OpenSSL', 'email.Generator', 'email.Iterators', 'email.Utils', 'pkg_resources', 'pywintypes', 'resource', 'win32api', 'win32con', 'win32event', 'win32file', 'win32pipe', 'win32process', 'win32security']

发送邮件的python源码
# -*- coding: utf8 -*-
'''
Created on 2012-9-10

@author : duxuefeng
'''
import smtplib
from email.MIMEText import MIMEText
from email.Header import Header

mailto_list=["*****@163.com"]
mail_host="smtp.163.com"
mail_user="******@163.com"
mail_pass="*****"
mail_postfix="163.com"
mail_sub="email测试"

def send_mail(to_list,sub,content):

    me='***@163.com'
    msg=MIMEText(content,_subtype='plain',_charset='utf-8')
    msg['subject']=Header(sub,'utf-8')
    try:
        server=smtplib.SMTP()
        server.connect(mail_host)
        server.login(mail_user,mail_pass)
        server.sendmail(me,to_list,msg.as_string())
        server.close()
        return True
    except Exception,e:
        print str(e)
        return False
if __name__=="__main__":
    mail_text = "你好,这是测试邮件"
    send_mail(mailto_list,mail_sub,mail_text)

编译源码:
'''
Created on 2012-9-10

@author : duxuefeng
'''
from distutils.core import setup
import py2exe
setup(console = ["mail.py"])


py2exe经常使用参数:
  1. Options for 'py2exe' command:  
  2.   --optimize (-O)       optimization level: -O1 for "python -O", -O2 for  
  3.                         "python -OO"and -O0 to disable [default: -O0]  
  4.   --dist-dir (-d)       directory to put final built distributions in (default  
  5.                         is dist)  
  6.   --excludes (-e)       comma-separated list of modules to exclude  
  7.   --dll-excludes        comma-separated list of DLLs to exclude  
  8.   --ignores             comma-separated list of modules to ignore if they are  
  9.                         not found  
  10.   --includes (-i)       comma-separated list of modules to include  
  11.   --packages (-p)       comma-separated list of packages to include  
  12.   --compressed (-c)     create a compressed zipfile  
  13.   --xref (-x)           create and show a module cross reference  
  14.   --bundle-files (-b)   bundle dlls in the zipfile or the exe. Valid levels  
  15.                         are 1, 2, or 3 (default)  
  16.   --skip-archive        do not place Python bytecode files in an archive, put  
  17.                         them directly in the file system  
  18.   --ascii (-a)          do not automatically include encodings and codecs  
  19.   --custom-boot-script  Python file that will be run when setting up the  
  20.                         runtime environment  
  21.   
  22. usage: setup_py2exe.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]  
  23.    or: setup_py2exe.py --help [cmd1 cmd2 ...]  
  24.    or: setup_py2exe.py --help-commands  
  25.    or: setup_py2exe.py cmd --help  
相关文章
相关标签/搜索