py2exe生成exe后,運行exe時提示No module named * 的解決辦法


一個pymssql 的程序在解釋器上運行正常,但是用py2exe打包后,提示

ImportError: No module named _mssql

百度了半天無果,然后bing,結果bing還是比百度靠譜,某神網的這段話,讓我有了靈感,英語真心不怎么好滴說:

FYI there is a separate newsgroup for py2exe at
gmane.comp.python.py2exe. You may want to post
there also.

Just as a suggestion, put an import decimal at
the top of your program. It looks like _mssql
might be doing dynamic imports in __load method
which will "confuse" py2exe because it can't
know about dynamic imports which happen at
runtime.

-Larry Bates

Chris wrote:
I've just completed a project using the following (Windows XP, python
2.4.1, wxpython 2.6, and pymssql 0.7.3). The program runs great, but
after I convert it to an exe (required for this project), it gives me
the following error when I try to run it.

Traceback (most recent call last):
File "EstUpdate.py", line 6, in ?
File "frmSplash.pyc", line 9, in ?
File "pymssql.pyc", line 23, in ?
File "_mssql.pyc", line 9, in ?
File "_mssql.pyc", line 7, in __load
ImportError: No module named decimal

However, when I look in c:\python24\lib on the machine which ran py2exe,
I see decimal.py and decimal.pyc.

Can someone please help with this? I'm supposed to start testing the
program today and I can't seem to move past this first step.

Thanks!!
Chris

 

 解決辦法就是這句話:

Just as a suggestion, put an import decimal at
the top of your program. It looks like _mssql
might be doing dynamic imports in __load method
which will "confuse" py2exe because it can't
know about dynamic imports which happen at
runtime.
運行exe的時候提示缺什么模塊就import什么模塊,並在代碼里面顯示的調用一次,我在程序里是調用了__version__,如下代碼:

1 # -*- coding:gbk -*-
2 import pymssql
3 import _mssql
4 import uuid
5 import decimal
6 
7 decimal.__version__
8 uuid.ctypes.__version__
9 _mssql.__version__

調用之后,打包完直接就可以運行了。

參考地址:http://bytes.com/topic/python/answers/168576-help-py2exe-error-no-module-named-decimal


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM