將發送郵件的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