Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add external exe file with pyinstaller

I have a python program that uses multiple files including an .exe program called with subprocess.run(myprogram.exe).

When I build the new .exe with pyinstaller and try to execute the program, it cannot find myprogram.exe.

I'm using the following statment:

pyinstaller -w -F --onefile .\MyPythonProgram.py

myprogram.exe is in the same folder as MyPythonProgram.py. Should I change anything for it to add it to the .exe?

like image 727
Angela Avatar asked Nov 04 '25 10:11

Angela


1 Answers

Before I answer, be wary when bundling executables into your programs because of implications related to copyrights, patents, legal, etc. I know the OP's question is for their own .exe, but this message is for other people reading this post. Don't ask me about this, I am not a lawyer.


Unless you copy your .exe over to your target environment, you have to include it in one of the arguments to pyinstaller. PyInstaller is great, but it isn't as smart as you're imagining it to be (e.g. it doesn't scan your code for calls to subprocess and automatically try and find your executable dependencies).

Instead, you have to explicitly tell it to include external dependencies:

pyinstaller.exe --add-binary ".\myprogram.exe;." --onefile .\MyPythonProgram.py

Next time, try reading their docs because you're using -F and --onefile (they both do the same thing).

This GitHub issue helped me answer your question. It explains why --add-binary's arg is in quotes as well as the need for the ;. part at the end.

Note: I've excluded your -w option for a cleaner answer

like image 140
Xavier L. Avatar answered Nov 07 '25 05:11

Xavier L.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!