Pyinstaller offer the possibility to create one file exe directly but generally it is triggered as a virus byt most of commons AV included windefender.
This is not very fair for sysadmin who need to deploy an home made exe.
But there is a solution with sfx archives.
Why ?
In the sysadmin life it is possible to work on many differents projects, clients.
The example of writing a script in python.
The script works like a charm, now, time is coming to deliver it to the windows machines.
It is still possible to deploy a python version on the machines, then deploy the script.
Ok, but the source is hosted on each machine, not a very good idea.
An other possibility, and the best one for me, is to turn the python script into an exe file, into One exe file it's the best.
How ?
One of the main solution found on internet is to use PyInstaller.
And yes, PyInstaller is very a very good tool but the worst thing is when the parameter --onefile is used, to get the equivalent of a static builded binary PyInstaller use some dirty tricks and this behavior is generally detected as a weird behavior during a static analysis and the exe is dropped.
Same result with other tool like py2exe (but I dont know py2exe so much).
There is an other interesting tool cx_Freeze.
cx_Freeze has not the capability for create a onefile exe exactly for the reason that PyInstaller is triggered.
I will change from PyInstaller to cx_Freeze because, both can create exe with linked libs but, when cx_Freeze create the exe,
it create a better organisation (all the process is available with PyInstaller too).
To put all files together in one single file SFX is used.
Build an SFX archive is very easy with the 7-Zip.
program
7-Zip provide many modules, the interesting one for the project is 7zS2.sfx. A direct download link to 7zS2.sfx.
Now this it's time to build the standalone exe.
Code !
To convert a python script, it is very easy.
Take this vert hard and complex sript:
welcome.py
name = input("Enter your name: ")
welcome_msg = 'Hello and Welcome {0} !!'.format(name)
print(welcome_msg)
input("Press [Enter] to exit")
Execution
C:\> python.exe welcome.py
Enter your name: operator
Hello and Welcome {0} !!
Press [Enter] to exit
Ok, now cx_Freeze is needed.
python.exe -m pip install cx_Freeze
Then create the exe form the python script
C:\> cxfreeze.exe welcome.py
The output is a directory build who contains the exe and the libs under build\exe.win-amd64-3.9
lib
python3.dll
python39.dll
welcome.exe
Execution
C:\> welcome.exe
Enter your name: operator
Hello and Welcome {0} !!
Press [Enter] to exit
The exe works fine, but not very convenient to deploy like this with many files.
This is when SFX come on the scene !
First step, download and install necessary packages.
Some files will be downloaded with bitsadmin.
Download 7-Zip and install it.
Download the archive containing the sfx module:
md %userprofile%\work
bitsadmin /transfer dlsfx /download /priority normal https://www.7-zip.org/a/lzma1900.7z %userprofile%\work\lzma1900.7z
Then extract it with 7-Zip to get the interesting file 7zS2.sfx
The next step is to create an 7z archive (Installer.7z) who will contains all the python libs and the exe genarated with cxfreeze.exe
"C:\Program Files\7-Zip\7z.exe" a -y Installer.7z lib python3.dll python39.dll welcome.exe
Now a config file config.txt is needed, who contains instructions for the installer.
;!@Install@!UTF-8!
Title="Welcome"
RunProgram="welcome.exe"
;!@InstallEnd@!
At this time there is 7zS2.sfx, Installer.7z and config.txt.
The final step is to put them together in a single exe file (the goal of this post).
For this the windows command copy is used with the option /B for binary.
copy /b 7zS.sfx + config.txt + Installer.7z welcome.exe
Now if we click on welcome.exe, a console appears and the program is started.
For efficient usage it is better to combine all these steps in a batch script
build_exe.bat
cxfreeze .\welcome.py
md exec
xcopy config.txt exec
xcopy 7zS.sfx exec
xcopy build\exe.win-amd64-3.9\* .\exec /E /H /C /I
cd exec
"C:\Program Files\7-Zip\7z.exe" a -y Installer.7z lib python3.dll python39.dll welcome.exe
copy /b 7zS.sfx + config.txt + Installer.7z welcome.exe
xcopy welcome.exe ..
cd ..
There is other possibilities maybe, but this one is quite easy and it is not triggered by AV, this is a big point for sysadmin