pyinstallerAs an excellent three-way library, you can package your program into a windows executable file (exe), the following answers to questions that may arise during use:
1、Packing process
The pyinstaller packaging process can be divided into two main parts:
1.1. Generate configuration files ()
First install the pyinstaller library on your computer, just install it via pip, and thenGo to the path where you want to package the program,
Let's say you want to pack, use the following command to generate a configuration file:
pyi-makespec
Then you can be in thepeer catalogViewed infile, which is the file you packaged with theconfiguration fileThis contains all the configurations related to packaging.
1.2 Generation of executable files ()
1.1 After execution, use the following command:
pyinstaller
This command packages all the environments you need to run the file.bear in mind!: The build and dist folders are generated during the packaging process. The dist folder contains a folder for your packaged program, which contains the program's runtime dependencies (equivalent to a backup of your environment, to ensure that it can still be used when ported to computers without the environment), e.g., the directory of my dist folder is as follows:
Figure 1 dist file directory
Figure 1 shows an example of what I do when I do my packaging, my dist folder containsbaseline_model_run_new folder (it is because the name of the program I packaged is baseline_model_run_new.py, the specific folder name is the same as the name of the program you packaged)The folder baseline_model_run_new is shown in Figure 1, which contains the exe file in the red box and many dependencies, which is the correct one.If you only have exe files in your dist folder, there is no way for others to run your exe files because there is no packaging environment, please follow 1.1 & 1.2 again.
2. Packaging program needs to read external data (additional reading data)
In the deep learning or machine learning process can not be avoided will be involved in reading additional data, then this piece of configuration is not good will lead to others to run your exe file when the effect of Figure 2:
Figure 2 Exe file data error
Figure 2 shows the program needs to read additional data errors, the program runs locally without any problems, but a packaged problems, a variety of files can not be found, this problem is mainly due to the data path as well as the configuration of the problem, directly on the code description.
block_cipher = None a = Analysis(['baseline_model_run_new.py'], ### The names are different ### pathex=[], binaries=[], datas=[('train_data.txt','.'), ('', '.')], ### The content is different ### hiddenimports=[], hookspath=[], runtime_hooks=[], excludes=[], win_no_prefer_redirects=False, win_private_assemblies=False, cipher=block_cipher)
The above code is part of the spec file.The name of the packaged file on the second line is automatically generated based on the py file you packaged.The difference is the list of datas in the fifth line.By default it is an empty listWhat documents are required?Add it yourself.For example, my program reads the train_data.txt file, which is stored in the following locations: the py file and the txt file.Sibling directory storage, then the writeup is as above, while the model files for deep learning may also be needed, ditto.
Then save the spec file and execute the following command:
pyinstaller
Recompile the spec file to package it, and the finished package must look similar to Figure 1.Be sure to include the relevant environment!!!
3、The problem of others running flashback
When you confidently send the packaged file to your classmate, he flashes back as soon as he opens it, and you've set the inputs for these as well, why is that? This piece isYou probably only sent the exe file.but (not)No relevant environmentAt this point, it's a good idea to putDist folder all compressedSend it to your classmate and then just ask him to execute the exe file to avoid the flashback problem .
summarize
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.