python3环境
支持Windows下和Linux下打包。
Pyinstaller安装
pip install pyinstaller
样例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import os import sys from flask import Flask, render_template from flask_cors import CORS
def resource_path(relative_path): if hasattr(sys, '_MEIPASS'): return os.path.join(sys._MEIPASS, relative_path) return os.path.join(os.path.abspath("."), relative_path)
app = Flask(__name__, static_url_path="", static_folder=resource_path('static'), template_folder=resource_path("templates"))
CORS(app, supports_credentials=True)
@app.route('/') def index(): return render_template('index.html')
app.run()
|
Pyinstaller打包示例代码(Win/Linux)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| from PyInstaller.__main__ import run import platform
if __name__ == '__main__': if platform.system() == "Windows": opts = ['app.py', '-n McBetterMonitor', '-F', r'--icon=.\Creeper.ico', '-y', '--clean', '--workpath=build', '--add-data=templates;templates', '--add-data=static;static', '--distpath=build', '--specpath=./' ]
run(opts) elif platform.system() == "Linux": opts = ['app.py', '-nMcBetterMonitor', '-F', r'--icon=./Creeper.ico', '--clean', '--add-data=templates:templates', '--add-data=static:static', ] run(opts) else: print(platform.system())
|
参考文章