2011-08-04 44 views
3

我有Python應用程序,應該以Windows可執行文件啓動。我正在使用py2exe和pymssql 1.9.908。pymssql引發ImportError:當使用py2exe構建應用程序時,沒有名爲_mssql的模塊

我用下一個構建腳本生成的應用程序:

from distutils.core import setup 
import MySQLdb 
import fnmatch 
import os 
import pymssql 
import shutil 
import py2exe 
import glob 

############## 

name = 'BGAgent' 
old_version = '0.1' 
ver = '0.1' 

distDir = 'Dist' + name + ver 
shutil.rmtree(distDir, True) 
shutil.rmtree('Dist' + name + old_version, True) 

os.mkdir(distDir) 

############## 

class Target(object): 
    """ A simple class that holds information on our executable file. """ 
    def __init__(self, **kw): 
     """ Default class constructor. Update as you need. """ 
     self.__dict__.update(kw) 

# MySQLdb 
#dst = os.path.join(distDir, "MySQLdb") 
#copy_tree(MySQLdb.__path__[0], dst) 
# pymssql 
site_packages_dir = os.path.dirname(pymssql.__file__) 
pymssql_files = []#'pymssql.py', 'pymssql.pyc', 'pymssql.pyo', '_mssql.pyd'] 
for eggInfo in glob.glob(os.path.join(site_packages_dir, '*mssql*')) : 
    pymssql_files.append(os.path.basename(eggInfo)) 

for fname in pymssql_files : 
    src = os.path.join(site_packages_dir, fname) 
    dst = os.path.join(distDir, fname) 
    if(os.path.isfile(src)) : 
     shutil.copy(src, dst) 
    else : 
     shutil.copytree(src, dst) 

includes = ['MySQLdb', 'pymssql', 'OpenSSL'] 
excludes = ['run_w.exe'] #['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger', 'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl', 'Tkconstants', 'Tkinter'] 

packages = ['MySQLdb', 'pymssql', 'OpenSSL'] 

dll_excludes = []#['libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll', 'tk84.dll'] 

data_files = ['server.pem', 
       'config.ini', 
       'run.bat', 
       #os.path.join(os.path.split(pymssql.__file__)[0], 'ntwdblib.dll'), 
       ] 
icon_resources = [] 
bitmap_resources = [] 
other_resources = [] 


MyApp_Target = Target(
    # what to build 
    script = "run.py", 
    icon_resources = icon_resources, 
    bitmap_resources = bitmap_resources, 
    other_resources = other_resources, 
    dest_base = name,  
    version = ver, 
    company_name = "", 
    copyright = "", 
    name = name, 
    ) 


setup(
    data_files = data_files, 
    options = {"py2exe": {"compressed": 0, 
          "optimize": 1, 
          "includes": includes, 
          "excludes": excludes, 
          "packages": packages, 
          "dll_excludes": dll_excludes, 
          "bundle_files": 3, 
          "dist_dir": distDir, 
          "xref": False, 
          "skip_archive": False, 
          "ascii": False, 
          "custom_boot_script": '', 
         } 
       }, 
    zipfile = r'library.zip', 
    console = [], 
    windows = [MyApp_Target], 
    service = [], 
    com_server = [], 
    ctypes_com_server = [] 
    ) 

建築工作,但我有錯誤,當我試圖啓動應用程序:

File "pymssql.pyo", line 12, in <module> 
    File "pymssql.pyo", line 10, in __load 
    File "_mssql.pxd", line 10, in init pymssql (pymssql.c:7370) 
ImportError: No module named _mssql 

_mssql.pyd和pymssql.pyd文件在可執行目錄中。

OS版本的Windows 7企業單位SP 1

回答

3

在你試圖導入(例如在A.py爲A.exe時)的程序,指定_mssql import語句爲好。您可能還需要導入其他幾個模塊(十進制& UUID)獲得EXE工作

+0

這是pymssql中的錯誤。如果我隱式地在套接字模塊之前導入了_mssql,所有工作都正常。我發現如何解決這個問題,不過謝謝。 – varela

2
from distutils.core import setup 

import py2exe, os, pymssql 
import decimal 

data_files = [] 
data_files.append(os.path.join(os.path.split(pymssql.__file__)[0], 'ntwdblib.dll')) 
py2exe_options = {"py2exe":{"includes": ['decimal'], 
       "dll_excludes":["mswsock.dll", 
       "powrprof.dll", 
       "user32.dll", 
       "shell32.dll", 
       "wsock32.dll", 
       "advapi32.dll", 
       "kernel32.dll", 
       "ntwdblib.dll", 
       "ws2_32.dll", 
       "oleaut32.dll", 
       "ole32.dll", 
          ], 
}} 

setup(console=["jobs_pcc_main.py"], options= py2exe_options, data_files=data_files) 
+0

嗨,我發佈我的setup.py,我遇到了同樣的問題,請參考這個setup.py,我認爲你的代碼會好的。 – Chris

+0

感謝您的提示。爲什麼包含ntwdblib.dll作爲數據文件並排除爲DLL?有什麼竅門?我的問題是套接字模塊。它看起來像pymssql monkey-patching它,我將它導入到pymssql之前。 – varela

1

只需添加語句import _mssql在您的文件。接下來,運行你的程序。當你得到同樣的東西時,只需在代碼中導入該模塊即可。這種方法適用於我。

相關問題