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.
Related
If this is happening to you, the error (In this case) is a broken installation of pyinstaller or python, remove it from the computer and do a fresh reinstallation.
When trying to create a exe with pyinstaller, it works fine and the bundled .dll file is included an it unpacks the _MEI folder with the necesarry files the correct places. BUT i creates one called _MEIxxx but tries to open a _MEIxxY which does not exist (yes, both changes name everytime it is launched)
I cannot see anywhere you can manually set the name of the _MEI folder which would make it much easier.
The cmd command i am running is:
pyinstaller script.py --add-data "PATH TO DLL\python39.dll;test" -F --runtime-tmpdir .
reproducible problem:
creating a fresh .py project with python 3.9(i use pycharm)
include code of:
print("HI")
then in cmd use:
pyinstaller main.py -F (we want it to be a onefile exe)
Then the .exe file is copied to another pc
Here we run it with CMD to see the error output.
It returns the error:
Error loading Python DLL: "path to local\Temp\_MEIXXXX\python39.dll
the error (In this case) is a broken installation of pyinstaller or python, remove it from the computer and do a fresh reinstallation.
edit: How To Use --add-data with an entire folder?
Im converting my .py file to .exe using pyinstaller. but when i run it, it gives me this error:
Failed To Execute Script "test.py" due to unhandled exception: Couldn't open "C:\Users\Dani\AppData\Local\Temp_MEI11962\button_start.png" no such file or directory
Failed To Obtain/Convert Traceback!
Therefore My Converted Python File (exe) Was in the same directory As My Image.
My path code is built like this:
from locate import this_dir
path = this_dir()
str(path) + "\\button_start.png"
A possible solution is to use pyinstaller's --add-data argument as shown in the documentation. You can either append this to your command when building the .exe or include that in a pyinstaller.spec file.
Additional information:
When you execute a .exe, that was created with pyinstaller, it will create a temporary folder that is named _MEI<some numbers>, just as you showed above. In there, it expects all the necessary files. Therefor, this is also the place, where it will search for the image, no matter where you put your .exe to or where you execute it. Since this folder is generated temporarily every time you start the .exe, it would not help to simply copy your image there.
I created an application that reads a text file to get a digit (1 or 0) to execute some code (it runs like a save file since I want the value to be changed permanently, not just while the script is running), but when I use pyinstaller to convert it to a standalone exe file I get a Fatal Error saying that it cannot run the script. Previous compilations from before I added this feature worked, so I know it's something to do with reading that text file.
Note: I tried using a .py file instead of a .txt. While it worked as a script, the same error came up after compilation.
this is what I type into an elevated command prompt:
pyinstaller --onefile --add-data save.txt;.' file.py
You can not edit the data files that you have bundled with your application using --add-data in pyinstaller's --onefile method as it unpacks all the files into a temporary directory when executed, therefore the file wiil not be found in the current directory as the application demands, so you end up with the Fatal Error.
To get around this, refer to this post, use that function in your code and send your relative path into it, so as to get the absolute path of the file when the application is run. Remember, the changes that you make will be lost after you close the application, as these files are treated as "data files", they are not intended to be modified, so the binary will create a new copy of initial file on every application launch.
If you want it to permanently be stored somewhere, you can implement the file creation (if it doesn't exist) form the script itself, by creating a directory in the AppData folder and save the file there.
I try using pyinstaller to convert *.py to *.exe and put them in system32 to try to make them a cmd command but I always get the error:
Cannot open self C:\WINDOWS\system32\file.exe or archive
C:\WINDOWS\system32\file.pkg
I tried reinstalling pyinstaller and Python and tried different commands and code but nothing works.
I made a dummy file to check if something was wrong with my code
print("test")
But that didn't work either.
the command I usually use is
pyinstaller --onefile file.py
That works great but not if I try using it as a command, I can open it as a file but not a command
I hope I can just type file and it executes but now I have to do
cd C:\Users\user\Downloads\file.exe
file.exe
I strongly recommend you do not put your file in System32.
Instead add the path of wherever you keep the .exe file to path.
See this post for how to add a directory to your path.
I wrote a program in Python that includes external files, including images and a GUI .ui file.
I want to convert the Python script to a single executable (.exe) file using PyInstaller. When I try to open the .exe file, the program does not open.
You will need more information on why the application is closing immediately in order to solve your problem. To view the error messages associated with running your executable, run the .exe file from the command prompt: /path/to/app/dist/MyApp.exe. This will allow you to observe any errors that may exist after the app was bundled. If the program fails during an import statement, you may need to add a package to the hiddenimports list in the .spec file. Check the PyInstaller documentation for instructions on modifying the .spec file.