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

Jaq*_*sos 0 python java jar pyinstaller

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

我正在尝试生成一个使用tabula-py lib的可执行文件。这个库需要一个 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')
Run Code Online (Sandbox Code Playgroud)

尽管如此,错误仍然存​​在。当我从命令行运行我的代码时,来自使用 java jar 的 tabula-py 的函数 read_pdf() 可以正常工作,完美无缺。

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

无法访问 jarfile C:\Users\jaquedeveloper\AppData\Local\Temp_MEI58442\tabula\tabula-1.0.1-jar-with-dependencies.jar

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

此处也报告此问题。

小智 6

不久前我偶然发现了这个线程,因为我遇到了完全相同的问题。使用 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 输出,并使用 jar 文件添加了一个目录“tabula”,并且它起作用了。

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

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
Run Code Online (Sandbox Code Playgroud)

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