PyInstaller打包Sanic记录

主要工具版本:

  • Python: 3.8.10
  • PyInstaller: 5.13.0
  • Sanic: 23.6.0

打包问题解决(启动报错)

1、找不到tracerite\style.css

FileNotFoundError: [Errno 2] No such file or directory:
 'C:\\Users\\admin\\AppData\\Local\\Temp\\_MEI188802\\tracerite\\style.css'

解决:
默认打包的过程中,不会带入资源文件。为了把依赖包里的内容携带完整,需添加如下参数:
--collect-all=tracerite

2、pkg_resources.DistributionNotFound

pkg_resources.DistributionNotFound: 
The 'html5tagger>=1.2.1' distribution was not found and is required by tracerite

解决:
添加如下参数:--copy-metadata=html5tagger

3、OSError: could not get source code

Traceback (most recent call last):
  File "main.py", line 43, in main
  File "mods\boot.py", line 47, in main
  File "sanic\mixins\startup.py", line 215, in run
  File "sanic\mixins\startup.py", line 958, in serve_single
  File "sanic\worker\serve.py", line 143, in worker_serve
  File "sanic\worker\serve.py", line 117, in worker_serve
  File "sanic\server\runners.py", line 222, in _serve_http_1
  File "asyncio\base_events.py", line 616, in run_until_complete
  File "sanic\app.py", line 1738, in _startup
  File "sanic\touchup\service.py", line 24, in run
  File "sanic\touchup\schemes\base.py", line 27, in build
  File "inspect.py", line 997, in getsource
  File "inspect.py", line 979, in getsourcelines
  File "inspect.py", line 798, in findsource
OSError: could not get source code

解决:
需添加如下参数:--collect-all=sanic

4、启动Sanic进程时,主脚本又执行了一次(一共两次)

Traceback (most recent call last):
  File "argparse.py", line 1800, in parse_known_args
  File "argparse.py", line 2009, in _parse_known_args
  File "argparse.py", line 1965, in consume_positionals
  File "argparse.py", line 1858, in take_action
  File "argparse.py", line 2409, in _get_values
  File "argparse.py", line 2446, in _check_value
argparse.ArgumentError: argument mod: invalid choice: 'parent_pid=19452' (choose from 'boot', 'cs')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 53, in main
  File "argparse.py", line 1768, in parse_args
  File "argparse.py", line 1807, in parse_known_args
  File "argparse.py", line 2521, in error
  File "argparse.py", line 2508, in exit
SystemExit: 2

报ArgumentError错误,是因为Sanic启动的参数,跟主脚本接受的参数不一致。根本问题是主脚本又执行了一遍。

解决 - 方案1:Sanic走单进程方式

def main():
    # 增加启动参数:workers=1, single_process=True
    app.run(host='0.0.0.0', port=8001, workers=1, single_process=True)

解决 - 方案2:AppLoader启动Sanic + freeze_support规避主脚本二次执行

# 主脚本:main.py
if __name__ == '__main__':
    # freeze_support:主脚本被二次执行时进程会退出
    multiprocessing.freeze_support()
    main()

# Sanic服务创建脚本:boot.py
import os
from sanic import Sanic
from sanic.response import text
from sanic.worker.loader import AppLoader

def attach_endpoints(app: Sanic):
    """ 在这里定义路由函数 """
    @app.route('/')
    async def index(request):
        return text("Hello")

def create_app() -> Sanic:
    app = Sanic("PL-sanic")
    attach_endpoints(app)
    return app

def main():
    loader = AppLoader(factory=create_app)
    app = loader.load()
    app.prepare(port=8001, dev=False, workers=int(os.cpu_count() / 2))
    Sanic.serve(primary=app, app_loader=loader)

OSError: cannot open resource

更新 spec 文件

PyInstallerspec 文件中,确保包含字体文件:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值