之前在网上搜了好多都是说bootstrap没有初始化,但我的程序是正常运行的,只是打包后就报错,解决方法是在spec文件中将flask_bootstrap中的bootstrap文件夹直接打包到templates文件夹下,spec文件内容如下
from PyInstaller.building.build_main import Analysis, PYZ, EXE, COLLECT, BUNDLE, TOC
block_cipher = None
added_files = [
('.\\templates', 'templates'),
('.\\static', 'static'),
('D:\\Python\\Python27\\Lib\\site-packages\\flask_bootstrap\\templates\\bootstrap','templates\\bootstrap')
]
a = Analysis(['start.py'],
pathex=['D:\\workspace\\kutiku'],
hiddenimports=[],
binaries=[],
datas=added_files,
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='kutiku',
debug=False,
strip=False,
upx=True,
console=True)
打包后程序运行起来会在tmp目录下生成MEIXXX的文件夹,里面就是你的templates文件夹以及其他的文件夹,在你的启动代码中指定下打包后文件的生成目录,这样,程序就知道去哪里找文件了,代码如下
if getattr(sys, 'frozen', False):
template_folder = os.path.join(sys._MEIPASS, 'templates')
static_folder = os.path.join(sys._MEIPASS, 'static')
app = Flask(__name__, template_folder=template_folder,static_folder=static_folder)
else:
app = Flask(__name__)
问题就解决了!!!