[Py] PyInstaller打包Sanic记录

2023-08-02  本文已影响0人  _Walker__

主要工具版本:

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

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)

参考文章:
Python生成exe文件的神器——PyInstaller (包括传参交互)
python用pyinstaller打包后,运行程序报错“pkg_resources.DistributionNotFound“的解决办法...
Sanic - Dynamic Applications
Python - multiprocessing

上一篇下一篇

猜你喜欢

热点阅读