This two days with Python wrote a gadget, want to do not install Python on the machine to execute, first thought of Pyinstaller.
Pyinstaller packaging programs usually require only one command:
1. Open the CMD console, the directory to switch to the Pyinstaller installation directory, my D:\python\Python36\Scripts directory;
2. Input command:pyinstaller D:\JetBrains\PyCharm\workspace\nginx management tools \main.py -f-w-i f:/picture/1.ico-n admin Tools-P D: \python\python36\lib\site-packages
Description: The red part is to package the main program path;
Optional parameters after the Red section:
-F: Packaged as a single file executable program, without this parameter there will be many other files with executables
-W: Whether it is a window program, not specified, the program runs when there is a console black window
-I: Icons for executable files
-N: Name of the executable file
-P: The search directory to include when packing, generally do not specify the item
Other parameters can be referenced Pyinstaller official documents.
3. If the configuration is no problem, hit the ENTER key, wait for a while to compile the completion. A dist directory is generated in the scripts directory where you can view the compiled executable program, as shown in the following procedure:
In general, this executable file can be run alone. But my program is a little bit more complicated, and my program is a flask-write Web program. Direct operation of the word has been reported template files can not find errors, the template file copied to the same directory does not work. Baidu did not find a solution, and finally Google came up with the solution: in the creation of the Flask App object to add this judgment can:
From flask import Flask
Import sys
import os
app = None
if getattr (sys, ' Frozen ', False):
template_ folder = Os.path.join (sys.executable, ' ... ', ' templates ')
Static_folder = Os.path.join (sys.executable, ' ... ', ') Static ')
app = Flask (__name__, Template_folder=template_folder, Static_folder=static_folder)
else:
app = Flask (__name__)
Reference Link: https://stackoverflow.com/questions/32149892/flask-application-built-using-pyinstaller-not-rendering-index-html
Next, recompile and copy the template files and executable programs to the same directory and run successfully.