One file executable generation with custom modified spec file in PyInstaller - python

I have a heavily modified spec file which I would like to compile using the --onefile option. I have several hidden imports and file path extensions but of course the standard Analysis, PYZ, EXE and COLLECT objects are still in there.
When I compile the .py file and allow pyinstaller to generate a spec file for me, using the --onefile option works and I do get one file executable.
When i compile my own .spec file with the --onefile option, pyinstaller fails to bundle everything up into one file. As far as I can tell it makes no difference, when using my own .spec file, whether I provide the --onefile option or now.
How can I compile my program into a one file executable, where I am providing my own .spec file?

Related

What is the build folder and spec file when making exe file using pyinstaller?

Q1. When I create an exe file with the pyinstaller, a build folder and a spec file are created, what are these?
Q2. Is there no problem in running exe file even if I delete these?
Q3. Or do I have to distribute these together when I distribute exe file to other computers?
Q4. additionally, what is the dist folder?
When I delete build folder, spec file and dist folder, the exe file executes well on my computer.
They are like temporary files and configuration files for PyInstaller. For a little advanced usage you can modify or manually create spec file with special instructions for build ..like modules to be included or excluded etc.
Yes after exe is generated you can erase them, keeping them usually speeds up your next build
you dont have to distribute them with exe.
unless its a standalone, you will need all files in dist folder along with exe for the program to execute on your computer. In case you built it as standalone you will need, exe file only.

Creating only a single standalone executable from .py file

Let's say I have an application with a .py file extension and I want to use PyInstaller to create it into a standalone executable with a .exe file extension. However I only want one .exe file that does not require dists nor a workpath.
Is it possible to use PyInstaller to create a standalone file that does not need a distpath nor workpath to function correctly?
I think the --onefile option is what you are looking for? This has worked fine for me in the past in order to create a single .exe file.

Can you access multiple photos with Pyinstaller?

Is it possible for pyinstaller to include multiple photos (.gif files). I use loads of photos in a Tkinter GUI, but I'm unsure whether or not this will work. My images are in different folders than the .py file, but I use the whole path when using the photos in my application.
I also use several .txt files in my program (kept in the same folder as the .py file), and a .wav file. Will my program still be able to access these?
Basically, is it possible to have an executable file that can access sound, image, and text files using pyinstaller.
I need to be able to distribute this as well. So, I need my exe to be able to access those photos, text files, and sound files when on another computer.
From my own experience, I guess you can move all the needed files into the folder where the executable is located (i.e. dist/) after building your application.
You can also do this in a single command: pyinstaller main.py --add-data <SRC;DEST or SRC:DEST> for adding additional non-binary files to the executable.
The path separator is platform specific, os.pathsep (which is ; on Windows and : on most unix systems) is used. This option can be used multiple times.
Alternatively, you can change the spec file generated by PyInstaller to add additional data files to your app.
Create a spec file using pyi-makespec main.py.
Change the spec file to include your data files:
a = Analysis(..., datas=[('path/to/your/file', '.'),], ...)
The first string specifies the file or files as they are in this system now.
The second specifies the name of the folder to contain the files at run-time.
After you have created a spec file and modified it as necessary, you build the application by passing the spec file to the pyinstaller command: pyinstaller main.spec.
Reference: https://pythonhosted.org/PyInstaller/spec-files.html

How to call Pyinstaller via a .bat file?

Currently to use Pyinstaller I open a Python command console then go to the location of my .spec file using cd and type pyinstaller Software.spec. It works well but I'd like to automate that and have a .bat file do those operations for me.
I have made small .bat files to execute my programs (see below) and I thought the structure for calling Pyinstaller would be close but I'm groping.
C:\Python\python-3.6.3.amd64\python Software.py
pause
Failed attempts of .bat files to run Pyinstaller include:
C:\Python\python-3.6.3.amd64\python\Scripts\pyinstaller.exe Software.spec
C:\Python\python-3.6.3.amd64\python CALL :pyinstaller Software.spec
Any idea would be welcome.
Solution
We need to execute Pyinstaller.exe with Python like so:
"path\to\python.exe" "path\to\pyinstaller.exe" "path\to\my\Software.spec"
Typically you need to call the path to the script as well, but also always double quote paths to ensure you do not get some whitespace kreeping in. Always call the full extension name of an executable as good measure.
"C:\Python\python-3.6.3.amd64\python.exe" "C:\path\to\Software.py"
You can also start it, but in the same batch window:
start /b "" "C:\Python\python-3.6.3.amd64\python.exe" "C:\path\to\Software.py"
or with pyinstaller.exe example:
"C:\Python\python-3.6.3.amd64\python\Scripts\pyinstaller.exe" "C:\path\to\Software.spec"
A simple drag and drop of any python file will result in a compiled exe file in the dist folder created from executing the pyinstaller script.
#echo off
PATH = "%PATH% + %USERPROFILE%\AppData\local\programs\python\Python38"
:: %1 is the file dropped on the batch file
pyInstaller --windowed --onefile %1
If you change %1 to the filename it will also work. Save the script as Pyinst.bat then drag and drop a python file onto it.

Pyinstaller tells me he created the .exe file but I can't find it

Well basically i did this:
pyinstaller --onefile --name=OrdnerErstellen --windowed "filepath"
And it worked out but then I copied the Path where my .exe file should be and my PC couldn't find the file. Does somebody know why?
It also didn't even create the dist folder where all these files are supposed to go...
Im really new to programming and I just wanted to try it out :)
EDIT: PyInstaller did make a compiled .pyc file for the one I wanted, but didn't create the .exe
Simple navigate to the folder where you kept your_script.py file , From there run on console pyinstaller --onefile your_script.py

Categories

Resources