python打包exe執行文件
1.pyinstaller 打包
-
這里以打包一個簡單fastapi服務為例子
-
環境
python 3.6.7
pyInstaller 4.7
FastApi 0.70.0
unicorn 0.15.0
- extra-hook/hoks-unicorn.py
from PyInstaller.utils.hooks import get_package_paths
datas = [(get_package_paths('uvicorn')[1], 'uvicorn')]
- 執行命令
pyinstaller -y --clean --additional-hooks-dir extra-hooks foo.py
- foo.spec添加
hiddenimports=['uvicorn.lifespan.off','uvicorn.lifespan.on','uvicorn.lifespan',
'uvicorn.protocols.websockets.auto','uvicorn.protocols.websockets.wsproto_impl',
'uvicorn.protocols.websockets_impl','uvicorn.protocols.http.auto',
'uvicorn.protocols.http.h11_impl','uvicorn.protocols.http.httptools_impl',
'uvicorn.protocols.websockets','uvicorn.protocols.http','uvicorn.protocols',
'uvicorn.loops.auto','uvicorn.loops.asyncio','uvicorn.loops.uvloop','uvicorn.loops',
'uvicorn.logging']
- 執行
pyinstaller foo.spec
參照:
2.cx_Freeze
- 這里以打包一個簡單fastapi服務為例子
- 環境
python 3.6.8
FastApi 0.70.0
unicorn 0.15.0
cx-Freeze 6.8.3
- 構建setup.py
import sys
from cx_Freeze import setup, Executable
sys.path.append(r'../')
os.environ['TYPE'] = "red"
build_exe_options = {'packages': ['uvicorn', 'fastapi'],# 指定包
'excludes': [],
'include_files': []# 包含的靜態文件
}
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
setup(name = 'runFastApi',
version = '1.0.0',
description = '測試fastapi部署服務',
options = {'build_exe': build_exe_options},
executables = [Executable('foo.py', base=base)])
- foo.py
from fastapi import FastAPI
import uvicorn
app = FastAPI(
title="SERVER",
description="",
version="1.0.0",
)
@app.get("/")
async def read():
return {"Hello": "World"}
if __name__ == '__main__':
uvicorn.run(
app=app,
host="0.0.0.0",
port=9192
)
3.pyarmor
- 這里以打包一個簡單fastapi服務為例子
- 環境
python 3.6.8
FastApi 0.70.0
unicorn 0.15.0
pyarmor 7.0.3
- 打包
pyarmor pack foo.py
- 可以通過可視化界面進行混淆打包
pip install pyarmor-webui
# 啟動
pyarmor-webui -p 9088