SoFunction
Updated on 2024-11-13

Using pyinstaller to package py file as exe method

write sth. upfront

When I was working on Daiso, because I needed to calculate various energies, temperatures, and a whole lot of other data of the alloy, in order to be able to bless the students who came after me, I decided to package the python program that I used to process the data into an exe, so that I could run my program on computers that didn't have a python environment installed on them. So I googled a bunch of ways on how to package it, tried py2exe and pyinstaller, and found that the latter is easier and more convenient. In order to help myself to have a tutorial when I want to use it again in the future, I wrote this blog as a souvenir.

pre-conditions

First we need two things: python version 3.4, and the pyinstaller module.

To install pyinstaller on windows with pip just type in cmd

pip install pyinstaller

Ready to go.

Start packing.

First we put the files we want to package in the folder Test, the contents of the file are as follows

# Input two numbers and find the sum of the two numbers.

x = input('Please enter the first number:')
y = input('Please enter the second number:')

z = float(x) +float(y)

print(' The sum is found to be: '+str(z))

z = input('Please press any key to exit...')

The absolute address of this folder isC:/Test

So let's first get into the folder via cmd, which means typing in

cd C:/Test

That's as far as we've gotten into this folder, and then it's time for the main event, it's time to pack it up.

Then enter the command in cmd

pyinstaller -F 

Where the -F parameter stands for packing everything into one exe file. Without this parameter it would be a whole bunch of files, so this amounts to easy copying and use.

Once the long string of messages flashes by and eventually stops, we can take another look at the contents of the folder, which has an extra dist folder that holds what we need:.

Once we open it, we can see the following interface

sample interface

Congratulations! Success.

A few potholes.

Of course, the small program packaged into exe who is not like moving three or four hundred megabytes, is supposed to figure a convenient, if too large, but also more trouble. So when I packaged the pyinstaller I found that the program will use all the modules for him to include.

When I used the numpy module, my exe program all of a sudden rose to 100 + M, which makes me very puzzled, I looked at the packaging information, it turns out that he put all sorts of PyQt5 module is also packaged into it, however, my program does not use PyQt5 module, after all, it is just a black box, and have not yet to write the interface for it.

Suppose the example I used was

import numpy as np
# Input two numbers and find the sum of the two numbers.
x = input('Please enter the first number:')
y = input('Please enter the second number:')
z = float(x) +float(y)
print(' The sum is found to be: '+str(z))
z = input('Please press any key to exit...')

The size of the generated exe is 100 megabytes.

So I have no choice but to give up using numpy.

So if you know how to control the libraries that pyinstaller packages in, I hope you can point me in the comments or private messages, I appreciate it! The above is the entire content of this article, I hope to help you learn, but also hope that you support me more.