Jaqueline Passos
Jaqueline Passos

Reputation: 1399

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

How can I force pyinstaller to use specific .jar file when packing as exe?

I am trying to generate an executable which uses tabula-py lib. This library requires a jar file, tabula-1.0.1-jar-with-dependencies.jar, which I have in my file.py folder. These are some modifications which are at 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')

Still, the error persists. When I run my code from command line, the function read_pdf(), from tabula-py, which uses the java jar, works all right, perfectly.

But when I generate the executable with pyinstaller spec command, it fails to execute this function, giving the error below:

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

The file is not under this folder, nor the tabula folder exists. I have the file under the same folder of the executable file.How can I force the script to use it? How can I import the jar from a specific path to the executable file, instead of using _MEI folder?

This issue was also repported here.

Upvotes: 1

Views: 3848

Answers (1)

Louis Wagner
Louis Wagner

Reputation: 106

I stumbled upon this thread a while ago, since I ran into the exact same Problem. Adding the .jar file to a 'onefile' executable with pyinstaller seems to place it in the wrong temporal directory - under ...AppData\Local\Temp_MEI58442\ tabula \tabula-1.0.1-jar-with-dependencies.jar. The added File seems to be placed in ...AppData\Local\Temp_MEI58442\tabula-1.0.1-jar-with-dependencies.jar. That's why it can't be found when executing the tabula.read_pdf() command. As a workaround I produced a onedir output with pyinstaller (instead of onefile) and added a directory 'tabula' with the jar file, and it worked.

Bundling this into a single file also works: It's actually all there in the pyinstaller docs (https://pyinstaller.readthedocs.io/en/stable/usage.html). When adding the binary .jar file you can specify the relative path of the file inside the temporary folder after ':' for Linux and ';' for Win Systems. Using just '.' as path places the file under ./Temp_MEI*/. So in my case on Win it looked like this:

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

This solution comes a little late, but i hope it still helps somebody.

Upvotes: 9

Related Questions