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

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

问题描述:

   以下代码为发送邮件的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:  
  --optimize (-O)       optimization level: -O1 for "python -O", -O2 for                           "python -OO"and -O0 to disable [default: -O0]     --dist-dir (-d)       directory to put final built distributions in (default                           is dist)     --excludes (-e)       comma-separated list of modules to exclude     --dll-excludes        comma-separated list of DLLs to exclude     --ignores             comma-separated list of modules to ignore if they are                           not found     --includes (-i)       comma-separated list of modules to include     --packages (-p)       comma-separated list of packages to include     --compressed (-c)     create a compressed zipfile     --xref (-x)           create and show a module cross reference     --bundle-files (-b)   bundle dlls in the zipfile or the exe. Valid levels                           are 1, 2, or 3 (default)     --skip-archive        do not place Python bytecode files in an archive, put                           them directly in the file system     --ascii (-a)          do not automatically include encodings and codecs     --custom-boot-script  Python file that will be run when setting up the                           runtime environment      usage: setup_py2exe.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]      or: setup_py2exe.py --help [cmd1 cmd2 ...]      or: setup_py2exe.py --help-commands      or: setup_py2exe.py cmd --help  

转载于:https://my.oschina.net/duxuefeng/blog/77737

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值