如何在可执行文件中包含带有 pyinstaller 的特定 .jar 文件?

How to include specific .jar files with pyinstaller in executable file?

如何在打包为 exe 时强制 pyinstaller 使用特定的 .jar 文件?

我正在尝试生成一个使用 tabula-py 库的可执行文件。这个库需要一个 jar 文件,tabula-1.0.1-jar-with-dependencies.jar,我在我的 file.py 文件夹中。这些是一些修改,位于 myfile.spec:

# this is for pandas lib
def get_pandas_path():
    import pandas
    pandas_path = pandas.__path__[0]
    return pandas_path

dict_tree = Tree(get_pandas_path(), prefix='pandas', excludes=["*.pyc"])
a.datas += dict_tree
a.binaries = filter(lambda x: 'pandas' not in x[0], a.binaries)

# this is for tabula-py
jar = 'tabula-1.0.1-jar-with-dependencies'
jar_path = 'C:\Users\jaquedeveloper\Documents\freelancer\bot\' + jar
coll = COLLECT(exe,
       a.binaries,
       a.zipfiles,
       a.datas,
       [(jar, jar_path, 'PKG')],
       strip=None,
       upx=True,
       name='test')

仍然,错误仍然存​​在。 当我从命令行 运行 我的代码时,使用 java jar 的 tabula-py 中的函数 read_pdf() 工作正常,完美。

但是当我用pyinstaller spec 命令生成可执行文件时,它无法执行此功能,并给出以下错误:

Unable to access jarfile C:\Users\jaquedeveloper\AppData\Local\Temp_MEI58442\tabula\tabula-1.0.1-jar-with-dependencies.jar

文件不在该文件夹下,tabula文件夹也不存在。 我在可执行文件 file.How 的同一文件夹下有该文件,我可以强制脚本使用它吗? 如何将 jar 从特定路径导入可执行文件,而不是使用 _MEI 文件夹?

此问题也已报告 here

我不久前偶然发现了这个线程,因为我 运行 遇到了完全相同的问题。使用 pyinstaller 将 .jar 文件添加到 'onefile' 可执行文件似乎将其放置在错误的临时目录中 - 在 ...AppData\Local\Temp_MEI58442\ tabula\tabula-1.0 下.1-jar-with-dependencies.jar。 添加的文件似乎放在...AppData\Local\Temp_MEI58442\tabula-1.0.1-jar-with-dependencies.jar。这就是执行tabula.read_pdf()命令时找不到的原因。 作为一种解决方法,我使用 pyinstaller(而不是 onefile)生成了一个 onedir 输出,并添加了一个目录 'tabula' 和 jar 文件,它起作用了。

将其捆绑到一个文件中也可以: 它实际上都在 pyinstaller 文档中 (https://pyinstaller.readthedocs.io/en/stable/usage.html)。添加二进制 .jar 文件时,您可以在 Linux 和 ';' 的 ':' 之后指定临时文件夹内文件的相对路径赢系统。只使用 '.'作为路径将文件放在 ./Temp_MEI*/ 下。所以在我的 Win 案例中,它看起来像这样:

pyinstaller --onefile --add-binary "C:/Users/Louis/Desktop/Github/pdf_reader/venv/Lib/site-packages/tabula/tabula-1.0.2-jar-with-dependencies.jar;./tabula/" myapp.py

这个解决方案来的有点晚,但我希望它仍然对某些人有所帮助。