Making it easy to use pyinstaller to create 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 launched not only on the OS in which Python itself was installed, it was decided to build exe. Having read a couple of topics on the hub and comments on them, I came to the conclusion that pyinstaller is perfect for these purposes. It is quite easy to use, but still some repetitive points can be reduced.

    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, this was the last revision)
    3. pywin32-214.win32-py2.6 is the latest version available on sourceforge. It is used by pyinstaller to determine dependencies (in general, they need to be ruled out from it, since the last update is dated 09)
    4. UPX 3.0.7 - use for file compression


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


    Due to the fact that pyinstaller is stored in my python folder (the default path), and the sources on another disk, I decided to reduce the shamanism procedure with paths in the console and constantly adding the same parameters using bat file, which will now be parsed .

    To begin with, I defined the goals:
    1. The detachment from the need to constantly set the path to MakeSpec and Build files, as well as the spec file
    2. Getting the finished exe in the same directory as the source code


    Implementation of the bat file itself (comments along the way, a finished batch file with comments is below):
    1. Standard for bat lines - cancel the output of commands to the console, clearing the latter:
    @echo off
    cls

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

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

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

    5. Actually the key / parameter labels themselves. The input variations are various, the essence is the same - if such a key exists, we perform the necessary manipulations, such as obtaining the name of the source file, the path along which the spec file will be stored, help output, and using the shift command, the parameters are shifted by 1 to the 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.
    echo Use:% ~ n0 [/ F [path] filename] [/ O dir] [/ M]
    echo.
    echo / F, -F, / file, --file - Path to .py file 
    echo / O, -O, / out, --out - Output directory. Default directory -% buildPath%
    echo / M, -M, / move, --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 whether the name of the source file has been entered. If not, we display a message about the need for a file name, the presence of help and we complete the work:
    : binBuild
    if% fileParam% == 0 echo No file to work with. Use / h for help & goto: EOF

    7. We set the parameters in accordance with which exe will be assembled. We take the syntax in the pyinstaller manual. 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, we create a spec file and create an exe based on it, using the paths to MakeSpec, Build above and the parameters for the first. There is one terrible crutch - given that I am not special in batch files, I found a way to select only its name from the path to the source file only using the loop (2nd line):
    "% pyinstPath% \ MakeSpec.py"% specParams% "% pyFile%"
    for %% i in (% pyFile%) do (set fileName = %% ~ ni)
    % pyinstPath% \ Build.py "% buildPath% \% fileName% .spec "

    9. If a parameter was set to move the binary to the source folder, use the crutch from point 8 to get it. And then just move the replacement file without confirmation:
    if% moveBin% == 1 (
    for %% i in (% pyFile%) do (set outDir = %% ~ pi)
    move / y "% buildPath% \ dist \% fileName% .exe"% outDir%
    )


    The finished bat file with comments is here .
    To use, just edit the lines in steps 2 and 7 in accordance with 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 a command in the console, just execute <command_name> /? and they will write to you more than I can describe here and I know. If you know how to get rid of some crutches or to facilitate / simplify the implementation of certain points, I will listen with pleasure. If you think that this topic should not be on the Python blog, but in some other, please indicate which one.

    Also popular now: