📜 ⬆️ ⬇️

Make it easier to use pyinstaller to create an exe

Recently I began my acquaintance with the Python language in order to use it for writing in a short time applications that perform the necessary task here and now. Since the planned applications could be run not only on the OS in which Python itself is installed, it was decided to build exe. After reading a couple of topics on Habré and comments to them, I came to the conclusion that pyinstaller would be perfect for this purpose. It is quite easy to use, but still some repetitive moments can be shortened.

To begin, I will describe the software used for this:
  1. CPython 2.6.5 . Ask why not 2.7 or 3.x? The pywin version described below, used for pyinstaller, is friendly only with versions up to and including 2.6
  2. pyinstaller 1.4 r854 (at the time of the start of use it was the last revision)
  3. pywin32-214.win32-py2.6 is the latest version available on sourceforge. Pyinstaller is used to determine dependencies (in general, they need to be rolled from it, since the last update dates back to 09 year)
  4. UPX 3.0.7 - use for file compression

In order to assemble an exe in accordance with the manual, you must:
  1. Run Configure.py, which will check the presence of all the components necessary for the assembly (perform when changing configurations, such as adding / deleting UPX, changing libraries, etc.)
  2. Using MakeSpec.py from the pyinstaller folder in accordance with the necessary parameters, create a spec file
  3. Using Build.py from the same folder, specify the path to the spec file, build the exe

Due to the fact that pyinstaller is stored in my python folder (the default path), and the sources are on a different disk, I decided to shorten the shamanism procedure with the paths in the console and constantly adding the same parameters using the bat file, which will now be parsed .
For starters, defined goals:
  1. Detaching from the need to constantly specify the path to the MakeSpec and Build files, as well as the spec file
  2. Getting the finished exe in the same directory as the source code

The implementation of the bat file itself (comments along the way, the finished batch file with comments is below):
1. Standard for bat lines - cancel output of commands to the console, cleaning the last:
@ echo off

2. Specify the path to the pyinstaller and the path to the folder where the spec and exe files will be stored:
set pyinstPath = c : \ python26 \ pyinstaller
set buildPath = % pyinstPath % \ bin

3. Setting the variable values ​​for checking the movement of the finished exe and checking whether the source file name is entered (in fact, flags):
set moveBin = 0
set fileParam = 0

4. We start checking the entered parameters. For processing call the label with the entered key (3rd line). After processing each parameter, we return to the label: ParamChk. If the parameters are over, go to the binary build label (2nd line):
: ParamChk
if "% 1%" == "" goto binBuild
goto p % 1

5. Actually the labels themselves keys / parameters. The input variations are varied, the essence is the same - if such a key exists, perform the necessary manipulations, such as obtaining the name of the source file, the path through which the spec file will be stored, output help and use the shift command to shift the parameters 1 left (there was a parameter% 2, became% 1, etc.):
: pF
: p / f
: p / file
: p - file
if not "% 2" == "" (
set pyFile = % 2
if not % fileParam % == 1 set fileParam = 1
shift & shift & goto ParamChk

: pO
: p / o
: p / out
: p - out
if not "% 2" == "" set buildPath = % 2
shift & shift & goto ParamChk

: pM
: p / M
: p / move
: p - move
set moveBin = 1
shift & goto ParamChk

: pH
: p / H
: p / help
: p - help
echo Use :% ~ n0 [/ F [path] filename] [/ O dir] [/ M]
echo / F, - F, / file, - file - Path to .py file
echo / O, - O, / out, - out - Output directory. Default directory - % buildPath %
echo / M, - M, / move, - move - created binary to .py file directory
goto : EOF

6. After checking and processing all the parameters, go to the label: binBuild and check if the source file name was entered. If not, print a message about the need for a file name, help, and exit:
: binBuild
if % fileParam % == 0 echo No file to work with. Use / h for help & goto : EOF

7. Set the parameters according to which the assembly exe will be made. The syntax is taken in the manual to the pyinstaller. In this case, the following parameters are specified - assembly into one file, compression using UPX + folder change for the finished exe:
set specParams = - F - X - o % buildPath %

8. Next, create the spec file and create an exe based on it, using the paths specified above to MakeSpec, Build and the parameters for the first one. There is one creepy crutch - given that I am not an expert on batch files, then I found a way to select from the path to the source file only with the help of a loop (2nd line):
"% pyinstPath% \ MakeSpec.py" % specParams % "% pyFile%"
for %% i in ( % pyFile % ) do ( set fileName = %% ~ ni )
% pyinstPath % \ Build.py "% buildPath% \% fileName% .spec"

9. In the event that the parameter to move the binary to the folder with the source code has been set, to get it we use the crutch from item 8. And then just move the file with the replacement without confirmation:
if % moveBin % == 1 (
for %% i in ( % pyFile % ) do ( set outDir = %% ~ pi )
move / y "% buildPath% \ dist \% fileName% .exe" % outDir %

The finished bat comment file is here .
To use it is enough to edit the lines in paragraphs 2 and 7 according to your location of the pyinstaller folder and the required parameters.

Disclaimer :
That's all. I would like to note that in the comments both here and in the bat file itself I did not describe the features of the command syntax, but simply described the sequence of actions performed. For help on the command in the console, it suffices to execute <command_name> /? and they will write more to you than I can describe and know here. If you know how to get rid of some crutches or to facilitate / simplify the implementation of certain items, I’ll be happy to hear. If you think that this topic should not be in the Python blog, and in some other one, please indicate which one.

Source: https://habr.com/ru/post/104589/

All Articles