You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Nuitka编译Python为EXE时遇TikToken未知编码错误求助

tiktoken + Nuitka编译后出现'unknown encoding'问题的解决思路

问题描述

  • Python脚本中使用tiktoken统计消息token数完全正常,但用Nuitka编译为单文件可执行文件后,触发unknown encoding错误
  • 尝试修改tiktoken_ext/openai_public.py改为本地加载cl100k_base.tiktoken文件,未解决问题
  • PyInstaller只需添加--hidden-import=tiktoken_ext.openai_public --hidden-import=tiktoken_ext即可修复,但Nuitka使用以下命令仍失败:
    python -m nuitka script.py --onefile --show-modules --include-package=tiktoken --include-package=tiktoken_ext --include-package=blobfile
    
  • 相关代码片段:
    def num_tokens_from_messages(messages, model):
        """Returns the number of tokens used by a list of messages."""
        try:
            encoding = tiktoken.encoding_for_model(model)
            print("getting encoding_for_mdel",encoding)
        except Exception as e:
            print("Warning: model not found. Using cl100k_base encoding.",e)
            encoding = tiktoken.get_encoding("cl100k_base")
            print("in exception",encoding)
    

解决思路

  • 强制指定编码文件本地路径并打包
    直接在代码中加载本地的cl100k_base.tiktoken文件,避免依赖tiktoken的远程下载或自动加载逻辑,编译时手动打包该文件:

    import tiktoken
    from tiktoken.load import load_tiktoken_bpe
    
    def num_tokens_from_messages(messages, model):
        """Returns the number of tokens used by a list of messages."""
        try:
            encoding = tiktoken.encoding_for_model(model)
        except Exception as e:
            # 手动加载本地编码文件
            enc_path = "cl100k_base.tiktoken"
            mergeable_ranks = load_tiktoken_bpe(enc_path)
            encoding = tiktoken.Encoding(
                name="cl100k_base",
                mergeable_ranks=mergeable_ranks,
                special_tokens={
                    "<|endoftext|>": 100257,
                    "<|fim_prefix|>": 100258,
                    "<|fim_middle|>": 100259,
                    "<|fim_suffix|>": 100260,
                    "<|endofprompt|>": 100276
                }
            )
        # 后续token统计逻辑...
    

    编译命令添加编码文件打包参数:

    python -m nuitka script.py --onefile --show-modules --include-package=tiktoken --include-package=tiktoken_ext --include-package=blobfile --include-data-files=cl100k_base.tiktoken=.
    
  • 添加hidden-import参数并验证模块打包
    参考PyInstaller的解决方案,给Nuitka添加--hidden-import参数,同时查看--show-modules输出确认模块是否被正确包含:

    python -m nuitka script.py --onefile --show-modules --include-package=tiktoken --include-package=tiktoken_ext --include-package=blobfile --hidden-import=tiktoken_ext.openai_public --hidden-import=tiktoken_ext
    

    检查输出中是否存在tiktoken_ext.openai_public,如果未出现,需手动确认模块路径是否正确。

  • 修改tiktoken_ext的加载逻辑
    直接修改tiktoken_ext/openai_public.py中的_download_gpt2_bpe函数,强制返回本地编码文件路径,确保编译时修改后的文件被打包:

    # 修改tiktoken_ext/openai_public.py内的对应函数
    def _download_gpt2_bpe():
        return "cl100k_base.tiktoken"
    

    同步用--include-data-files参数打包编码文件。

  • 用standalone模式排查资源打包问题
    先放弃--onefile,使用--standalone生成文件夹形式的可执行文件,检查文件夹内是否包含tiktoken相关模块和编码文件。如果文件夹模式能正常运行,再排查单文件模式下的资源解压路径问题。

内容的提问来源于stack exchange,提问作者Aspoofer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 22:34:55