将发送邮件的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
'''
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
'''
from distutils.core import setup
import py2exe
setup(console = ["mail.py"])
py2exe经常使用参数:
- 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