使用py2exe打包包含pymssql,pyodbc等连接数据库模块出错解决方法

若是在setup.py打包程序中使用简单代码:html

# setup.py
from distutils.core import setup
import py2exe
setup(windows=["test.py"])

在dist文件夹中运行test.exe文件会出现以下错误:python

Traceback (most recent call last):
File "test.py", line 2, in
import pymssql
File "pymssql.pyc", line 30, in
File "_mssql.pyc", line 12, in
File "_mssql.pyc", line 10, in __load
ImportError: No module named decimalc++

能够把setup.py文件修改为以下:redis

# setup.py
from distutils.core import setup
import py2exe,os,pyodbc
py2exe_options = { "includes": "decimal, datetime" } } setup(windows=["test.py"],options={"py2exe": py2exe_options})

再到cmd的窗口使用python setup.py py2exe命令,通常可正常运行。在网上能够找到关于图形界面的打包脚本,例如:sql

包含图形界面(pyqt)的程序打包:windows

#setup.py

# -*- coding: utf-8-*-

from distutils.core import setup
import py2exe,sys,os
includes = ["encodings", "encodings.*"]

origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
        if os.path.basename(pathname).lower() in ("QtSvg4.dll"):
                return 0
        return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL

setup(windows=[{'script':'RunManager.py','icon_resources':[(1,"icon.ico")]}],
       options={'py2exe': {'includes':['sip','PyQt4._qt','PyQt4.QtCore','PyQt4.QtSvg','PyQwt'],
                "optimize": 0,
                "includes": includes
                #,"bundle_files": 1
                          }},
      data_files=["icon.ico"])

操做步骤:app

一、Dos窗口进入本目录
二、运行命令 python setup.py py2exe --includes PyQt4.QtSvg,sip
三、生成文件在dist目录
四、须要手动拷贝到dist目录的文件:images(目录);app.init;打包相关文件中的PyQt四、qt.conf、QtSvg4.dll;ui

本身修改其中的一些语句,就能够打包你想要的任何python脚本。spa

补充,有些PC机可能会出现找不到MSVCP90.DLL文件,用了图形模块的缘故吧。有以下几种解决方法:code

解决办法:

由于包含wxpython模块,需下载MSVCP90.DLL 并拷贝到Python26/DLLs 目录下

1.直接下载安装微软vc++ 2008 分发包http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf 

会安装到:

C:/WINDOWS/WinSxS/x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30411.0_x-ww_71382c73目录下

可能不一样的系统会稍有不一样,但C:/WINDOWS/WinSxS/x86_Microsoft.VC90...这个是一致的。

并且直接在windows里搜索搜不到。

2.单独下载MSVCP90.DLL 并拷贝到PythonXX/DLLs 目录下

http://www.dll-files.com/dllindex/dll-files.shtml?msvcp90

3.因为本机也能运行,确认不须要msvcp90.dll,能够直接在脚本中去掉包含该dll。但可能在其余人机器不能运行。

在py2exe的setup.py包含以下的代码:

distutils.core.setup( options = { "py2exe": {  "dll_excludes": ["MSVCP90.dll"] } }

 

from distutils.core import setup 
import py2exe 
setup(windows=['main.py'],  options = { "py2exe":   {"dll_excludes":["MSVCP90.dll"]}})

  4.若是已经安装msvc++ 2008以上,而找不到目录,能够以下设置,找到分发目录。


import sys 
sys.path.append('c:/Program Files/Microsoft Visual Studio 9.0/VC/redist/x86/Microsoft.VC90.CRT')
相关文章
相关标签/搜索