Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use bundled program after pyinstaller --add-binary?

Tags:

pyinstaller

I am trying to make an executable with pyinstaller, by issuing something like this:

pyinstaller -F --add-binary="sometool.exe:." myapp.py

The build works fine. But, if I try to do something like:

os.popen('sometool.exe'), the error is that was not found.

So, how can I do that?

like image 248
Fellipe Theophilo Barata Avatar asked Jan 11 '18 15:01

Fellipe Theophilo Barata


2 Answers

For Unix like machine

pyinstaller --noconfirm --log-level=WARN \
    --onefile --nowindow \
    --add-data="README:." \
    --add-data="image1.png:img" \
    --add-binary="libfoo.so:lib" \
    --hidden-import=secret1 \
    --hidden-import=secret2 \
    --upx-dir=/usr/local/share/ \
    myscript.spec

Or for Windows

pyinstaller --noconfirm --log-level=WARN ^
    --onefile --nowindow ^
    --add-data="README;." ^
    --add-data="image1.png;img" ^
    --add-binary="libfoo.so;lib" ^
    --hidden-import=secret1 ^
    --hidden-import=secret2 ^
    --icon=..\MLNMFLCN.ICO ^
    myscript.spec

Official Doc: https://pyinstaller.readthedocs.io/en/stable/usage.html

I spent hours to figure out how to use --add-binary and finally got it working. Look at --add-binary="libcrypto.dll:lib", you must add :lib as postfix.

like image 182
Feng Liu Avatar answered Oct 25 '22 02:10

Feng Liu


Try using this according to this question:

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
    return os.path.join(base_path, relative_path)

And than in your app:

os.popen(resource_path('sometool.exe'))

This should work. I use this everyday :)

like image 39
Jakub Bláha Avatar answered Oct 25 '22 03:10

Jakub Bláha