PyInstaller打包exe跨PC运行报FileNotFoundError问题咨询
问题背景
使用PyInstaller将自行编写的、可实现「PDF转可操作DataFrame再重新输出为PDF」功能的Python脚本打包为exe文件。最初遇到的tabula与PyInstaller不兼容问题,已通过修改spec文件解决,但目前程序运行触发FileNotFoundError,具体报错信息如下:
Traceback (most recent call last): File "room_list.py", line 7, in <module> File "tabula\io.py", line 314, in read_pdf FileNotFoundError: [Errno 2] No such file or directory: 'room_list.pdf' [8608] Failed to execute script 'room_list' due to unhandled exception!
在执行打包操作的原Windows 11系统PC上运行程序无任何异常,但将exe转移至另一台Windows 10系统PC,按照完全相同的目录结构将room_list.pdf放置在桌面后运行,仍出现上述报错,已确认room_list.pdf确实存在于对应路径下,暂不清楚报错原因。
项目源码
import tabula as tb import pandas as pd import os import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages df_list = tb.read_pdf('room_list.pdf', pages='all') concat_list = [] for df in df_list: new_df = df.iloc[3:] new_df.columns = df.iloc[2] concat_list.append(new_df) df_all = pd.concat(concat_list) df_all.dropna(inplace=True) def stay_over_status(row): if row['Occ Status'] == 'In-House': return 'X' else: return ' ' def check_out_status(row): if row['Occ Status'] == 'Due-Out': return 'X' else: return ' ' df_all['S/O'] = df_all.apply(stay_over_status, axis=1) df_all['C/O'] = df_all.apply(check_out_status, axis=1) df_all['OUT'] = '' result = df_all.drop(columns=['Room Type', 'Occ Status', 'Condition', 'Guest Name', 'Arrives', 'Departs']) fig, ax =plt.subplots(figsize=(12,4)) ax.axis('tight') ax.axis('off') plt.tight_layout() the_table = ax.table(cellText=result.values,colLabels=result.columns,loc='center', colWidths=[0.1 for x in result.columns], cellLoc='center') pp = PdfPages("maid_list.pdf") pp.savefig(fig, bbox_inches='tight') pp.close() # os.startfile("maid_list.pdf", "print")
已尝试的解决方案
- 在Windows 11虚拟机中复现了上述问题,多轮测试发现:将代码中
room_list.pdf的相对路径替换为PC专属绝对路径(如C:\Users\User\Desktop\room_list.pdf)可解决输入PDF找不到的报错,但后续会触发tabula依赖Java的相关报错。 - 在虚拟机环境安装Java后,Java依赖报错消失,但输出文件
maid_list.pdf无法生成;将输出文件路径也替换为虚拟机桌面的绝对路径后,程序可正常生成目标PDF,无报错。
核心诉求
后续会在目标Windows 10 PC上验证上述绝对路径方案,但期望达成多设备免配置部署的效果:无需在目标设备额外安装Java等程序、无需针对不同设备的用户目录修改代码重新打包exe。现咨询两个方向的可行方案:
- 是否存在无需依赖tabula和Java即可实现PDF转DataFrame的方案?
- 是否存在可行的PyInstaller打包配置方案,可让打包后的exe正确识别相对路径、内置相关依赖,无需目标设备额外配置环境即可运行?
内容的提问来源于stack exchange,提问作者jp207
相关产品推荐
相关产品推荐

