Skip to main content
 首页 » 编程设计

python之使用 py2exe 编译失败后从 Python 调用 MS Access 模块函数

2025年05月04日10del

我已经运行下面的 python 代码大约一年了。但我即将开始一份新工作。为了方便我即将离开的部门,我想将这段代码编译成.exe文件。

我已经尝试针对 Python 2.5 和 2.7 进行编译,结果相同。

但是当运行exe文件时出现如下错误

H:\development\DMS_Import_Data\dist>import_data.exe 
Run Live or Development Import (L = Live, D = Dev, X = Exit):l 
5:3:2012 10:56 Opening Live Database... 
5:3:2012 10:56 Assuming Database already open 
5:3:2012 10:56 Importing latest DMS Data... 
Traceback (most recent call last): 
File "import_data.py", line 79, in <module> 
File "import_data.py", line 46, in run 
File "<COMObject Access.Application>", line 2, in run 
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, u'You made 
an illegal function call.', None, -1, -2146820336), None) 

这是python代码。问题出在这个 block 的最后一行 datatype = conn.run("import data")

import win32com.client 
import pythoncom 
import time 
import datetime 
import os 
import string 
class ProcessDMSData: 
    sleepTime = 3600 
    ref_date = '' 
 
    def set_ref_date(self,ref_date): 
 
        self.ref_date = ref_date 
 
 
 
    def run(self): 
 
        run_type = "" 
 
        while True: 
            pythoncom.CoInitialize() 
            check_values = ["L", "D", "X"] 
            while run_type.upper() not in check_values:            
                run_type = raw_input ("Run Live or Development Import (L = Live, D = Dev, X = Exit):") 
            if run_type.upper() == "L": 
 
                update_type = "Live" 
 
                database_path = "G:\\Eurofighter programme Management\\1.PM Reporting\\Production Tracking Database\\booking_planeset_tracker_data.mdb" 
 
            elif run_type.upper() == "D": 
 
                update_type = "Development" 
 
                database_path = "G:\\Eurofighter programme Management\\1.PM Reporting\\Production Tracking Database\\dev_dms_tracker_data.mdb" 
 
            elif run_type.upper() == "X": 
 
                quit() 
            print "%s Opening %s Database..." % (self.get_date_time(), update_type) 
 
            conn=win32com.client.Dispatch('Access.Application' ) 
 
            try: 
 
                conn.OpenCurrentDatabase(database_path) 
 
            except: 
 
                print "%s Assuming Database already open" % (self.get_date_time()) 
                pass 
 
            print "%s Importing latest DMS Data..." % (self.get_date_time()) 
 
            datatype = conn.run("import_data") 

任何人都可以帮助解释为什么它可以在 python 中作为脚本运行,但当它被编译为 exe 时却不能。

谢谢 西蒙

请您参考如下方法:

根据 Juri Robi 的建议,我下载了 Pyinstaller 并编译了 exe。这在没有任何调整的情况下工作得很好。我仍然不知道为什么 py2exe 不起作用,但对我来说 Pyinstaller 是一个很棒的新工具,我将来会再次使用它。