Fork me on GitHub

python打包exe三种方式

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
posted @ 2021-11-25 13:45  是阿凯啊  阅读(1521)  评论(0编辑  收藏  举报