You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInstaller打包后pyodbc密码验证失效及跨用户运行崩溃问题排查

问题分析与解决方案:PyInstaller打包SQL连接程序后的权限&驱动问题

我用PyInstaller把这段Python代码打包成单文件程序后,自己运行时输入任意密码都能正常执行并生成正确的Excel文件;但其他用户在共享服务器运行时,输入密码后程序直接崩溃。密码本应是用户专属验证信息,为什么会出现这种情况?

原代码

import pyodbc
import pandas as pd
from pandas import ExcelWriter
import sys
import time
import getpass
username = input('Please enter username starting with COMPANY\\: ')
password = getpass.getpass('Please enter your password: ')
cnxn = pyodbc.connect(
 'Driver={ODBC DRiver 17 for SQL Server};'
 'Server=COMPANY;'
 'Database=COMPANY;'
 'uid=' + username + ';pwd=' + password + ';Trusted_connection=yes')
df = pd.read_sql_query(
 'doesnt matter, hiding for business', cnxn)
df['Counts'] = df.groupby(['DocumentNo'])['ItemNo'].transform('count')
df2 = df[df['Counts'] == 1]
df3 = df2[['ItemNo','DocumentNo','QO']].copy()
df3['Counts'] = df3.groupby(['ItemNo'])['DocumentNo'].transform('count')
df3 = df3[df3['Counts'] > 1]
df4 = df3.groupby(['ItemNo']).QO.sum().reset_index()
df5 = df3.merge(df4, on='ItemNo', how='left')
df6 = df5.groupby(['ItemNo','QO_y'])['DocumentNo'].apply(lambda x: ' | '.join(x.astype(str))).reset_index()
location = input('Please put the exact folder location where you want to put the excel file, and name of file: ')
writer = ExcelWriter(location)
df6.to_excel(writer,'Data')
writer.save()
time.sleep(10)
sys.exit()

核心问题拆解

1. 连接字符串的致命矛盾:混合两种验证方式

你在连接字符串里同时写了uid=...;pwd=...和Trusted_connection=yes,这直接搞乱了SQL Server的验证逻辑:

  • 对你来说,Trusted_connection=yes优先生效——程序直接用你当前的Windows账号身份登录数据库,完全忽略了你输入的用户名和密码!这就是为什么你输任意密码都能跑通的原因,因为你的Windows账号本来就有数据库访问权限。
  • 其他用户在共享服务器运行时,他们的Windows账号没有数据库访问权限,而程序又因为Trusted_connection=yes跳过了SQL账号验证,直接导致连接失败崩溃。

2. 驱动拼写错误+依赖缺失

你写的驱动名称是ODBC DRiver 17 for SQL Server,这里少了一个字母i(正确是Driver),这个拼写错误可能导致其他机器找不到驱动;另外,其他用户的机器可能根本没安装ODBC Driver 17,打包后的程序也没包含驱动文件,直接触发崩溃。

3. 共享目录的写入权限问题

如果用户选择保存到共享服务器目录,他们可能没有该目录的写入权限,程序在保存Excel时出错崩溃——但因为是打包后的单文件程序,错误信息被隐藏了,看起来像是输入密码后直接崩溃。


分步解决方案

第一步:修复连接字符串,明确验证方式

既然你需要用用户专属账号密码验证,必须移除Trusted_connection=yes,同时修正驱动名称的拼写错误:

cnxn = pyodbc.connect(
 'Driver={ODBC Driver 17 for SQL Server};'
 'Server=COMPANY;'
 'Database=COMPANY;'
 'uid=' + username + ';pwd=' + password + ';'
)

第二步:解决驱动依赖问题

  • 简单方案:让其他用户安装微软官方的ODBC Driver 17 for SQL Server;
  • 打包包含驱动:如果要让程序自带驱动,打包时把驱动dll文件加进去,命令示例:
pyinstaller --onefile --add-binary "C:\Windows\System32\msodbcsql17.dll;." your_script.py

注:32位系统的驱动文件路径是C:\Windows\SysWOW64\msodbcsql17.dll

第三步:添加错误捕获,暴露崩溃原因

在代码里加try-except块,捕获连接和保存时的错误,让用户能看到具体问题:

try:
    cnxn = pyodbc.connect(
     'Driver={ODBC Driver 17 for SQL Server};'
     'Server=COMPANY;'
     'Database=COMPANY;'
     'uid=' + username + ';pwd=' + password + ';'
    )
    # 后续数据处理、保存文件的代码都放到这里面
except Exception as e:
    print(f"出错了:{str(e)}")
    input("按回车键退出...")
    sys.exit()

第四步:验证共享目录权限

让用户先测试保存到本地目录,如果能正常生成文件,说明是共享目录的权限问题,联系管理员给用户分配该目录的写入权限即可。

内容的提问来源于stack exchange,提问作者bohzwah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 08:32:36