我拥有32位访问权限和64位Python(在x64 Windows 10上)。
我成功运行以下代码从.accdb数据库中获取表(或查询),并在我的Pycharm中获得所需的结果。
import pyodbc
import pandas
import os
import sys
sources = pyodbc.dataSources()
dsns = list(sources.keys())
dsns.sort()
sl = []
for dsn in dsns:
sl.append('%s [%s]' % (dsn, sources[dsn]))
print('\n'.join(sl))
print(pyodbc.drivers())
try:
currdir = os.path.abspath(__file__)
except NameError: # We are the main py2exe script, not a module
import sys
currdir = os.path.abspath(os.path.dirname(sys.argv[0]))
DBfile = os.path.join(currdir, 'UNION.accdb')
cnxn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s;' %DBfile)
sql = "Select * FROM topivot"
df = pandas.read_sql(sql,cnxn)
print(df)
输出列出了所有正确的驱动程序。
C:\Users\xxx\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/xxx/AppData/Local/Programs/Python/Python37-32/pyddb3.py
Excel Files [Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)]
MS Access Database [Microsoft Access Driver (*.mdb, *.accdb)]
UNION [Microsoft Access Driver (*.mdb)]
accdb [Microsoft Access Driver (*.mdb)]
dBASE Files [Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)]
['Driver da Microsoft para arquivos texto (*.txt; *.csv)', 'Driver do Microsoft Access (*.mdb)', 'Driver do Microsoft dBase (*.dbf)', 'Driver do Microsoft Excel(*.xls)', 'Driver do Microsoft Paradox (*.db )', 'Microsoft Access Driver (*.mdb)', 'Microsoft Access-Treiber (*.mdb)', 'Microsoft dBase Driver (*.dbf)', 'Microsoft dBase-Treiber (*.dbf)', 'Microsoft Excel Driver (*.xls)', 'Microsoft Excel-Treiber (*.xls)', 'Microsoft ODBC for Oracle', 'Microsoft Paradox Driver (*.db )', 'Microsoft Paradox-Treiber (*.db )', 'Microsoft Text Driver (*.txt; *.csv)', 'Microsoft Text-Treiber (*.txt; *.csv)', 'SQL Server', 'ODBC Driver 13 for SQL Server', 'SQL Server Native Client 11.0', 'Microsoft Access Driver (*.mdb, *.accdb)', 'Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)', 'Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)', 'Microsoft Access Text Driver (*.txt, *.csv)']
但是,当我通过自动Py-to-Exe(或Pyinstaller,通过避免最大递归错误并从.spec文件创建EXE)运行它时,出现以下错误:
'SQL Server', 'ODBC Driver 13 for SQL Server', 'PostgreSQL ANSI(x64)',
'PostgreSQL Unicode(x64)', 'Amazon Redshift (x64 I', 'SQL Server Native
Client 11.0', 'SQL Server Native Client RDA 11.0'] raceback (most recent
call last): File "pyddb2.py", line 14, in <module> .yodbc.InterfaceError:
('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not
found and no default dri er specified (0) (SQLDriverConnect)') 21444] Failed
to execute script pyddb2
这似乎列出了Windows ODBC 64驱动程序。为了防止这种情况,我尝试使用
的ODBC数据源(32位)还原ODBC数据源(64位)的目标路径。%windir%\ system32 \ odbcad32.exe
到
%windir%\ syswow64 \ odbcad32.exe
但无济于事。
为什么我设法在32位Access和64位Python上成功运行PY文件,但是如何创建可以连接的EXE。有什么想法吗?
答案 0 :(得分:1)
找不到连接正确驱动程序的方法,因此我重新安装了Access Engine,Python和Conda。安装了Python 32位,Access Engine 2016 32位以使其与Access 32bit匹配。安装了自动Py To Exe,它起作用了。 EXE文件现在具有64MB。