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

问题描述 投票:0回答:1

作为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')

仍然,错误仍然存​​在。当我从命令行运行代码时,使用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文件夹?

此问题也被认为是here

java python jar pyinstaller
1个回答
0
投票

前一段时间,我偶然遇到了这个问题,因为我遇到了完全相同的问题。使用pyinstaller将.jar文件添加到“ onefile”可执行文件中,似乎将其放置在错误的临时目录中-... AppData \ Local \ Temp_MEI58442 \ tabula \ tabula-1.0.1-jar-with-dependencies下。罐。添加的文件似乎放在... 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 Systems。仅使用“。”作为路径将文件放置在./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

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

© www.soinside.com 2019 - 2024. All rights reserved.