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

iOS集成Python遇ModuleNotFoundError: No module named 'encodings'求助

iOS集成Python时encodings模块缺失问题解决

问题背景

按照Python-Apple-support 3.9版本文档在iOS项目中集成Python,编译成功但调用时出现ModuleNotFoundError: No module named 'encodings'错误,相关代码及错误日志如下:

现有代码

func RunPythonScript() -> PythonObject {
    
    if let path = Bundle.main.path(forResource:"/Users/projects/extra/python_apple_support/PAS_10_11_v3/PAS_10_11_v3/Resources/",
                    ofType: nil) {
        setenv("PYTHONPATH", path, 1)
        setenv("PYTHONHOME", path, 1)

      }
    
    let sys = Python.import("sys")
    sys.path.append("/Users/projects/extra/python_apple_support/PAS_10_11_v3/PAS_10_11_v3/PAS_10_11_v3/")
    let file = Python.import("pythonscript")
    
    let response = file.hello_world()
    print(response)
    return response
}

错误日志

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = 'python3'
  isolated = 0
  environment = 1
  user site = 1
  import site = 1
  sys._base_executable = '/Users/karimkhan/Library/Developer/CoreSimulator/Devices/C444D135-C393-4631-AFE2-FF5F86935EF6/data/Containers/Bundle/Application/642E9540-CBAF-448C-8E8D-856B8E5D03EC/PAS_10_11_v3.app/PAS_10_11_v3'
  sys.base_prefix = '/Users/runner/work/Python-Apple-support/Python-Apple-support/install/iOS/iphonesimulator.x86_64/python-3.9.14'
  sys.base_exec_prefix = '/Users/runner/work/Python-Apple-support/Python-Apple-support/install/iOS/iphonesimulator.x86_64/python-3.9.14'
  sys.platlibdir = 'lib'
  sys.executable = '/Users/karimkhan/Library/Developer/CoreSimulator/Devices/C444D135-C393-4631-AFE2-FF5F86935EF6/data/Containers/Bundle/Application/642E9540-CBAF-448C-8E8D-856B8E5D03EC/PAS_10_11_v3.app/PAS_10_11_v3'
  sys.prefix = '/Users/runner/work/Python-Apple-support/Python-Apple-support/install/iOS/iphonesimulator.x86_64/python-3.9.14'
  sys.exec_prefix = '/Users/runner/work/Python-Apple-support/Python-Apple-support/install/iOS/iphonesimulator.x86_64/python-3.9.14'
  sys.path = [
    '/Users/runner/work/Python-Apple-support/Python-Apple-support/install/iOS/iphonesimulator.x86_64/python-3.9.14/lib/python39.zip',
    '/Users/runner/work/Python-Apple-support/Python-Apple-support/install/iOS/iphonesimulator.x86_64/python-3.9.14/lib/python3.9',
    '/Users/runner/work/Python-Apple-support/Python-Apple-support/install/iOS/iphonesimulator.x86_64/python-3.9.14/lib/lib-dynload',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000000108fd4600 (most recent call first):
<no Python frame>

问题原因

  1. 环境变量设置无效:Bundle.main.path(forResource:)不能传入绝对路径,导致path为nil,PYTHONHOME和PYTHONPATH根本没被设置。
  2. Python运行时路径错误:日志显示Python期望的是编译时的runner路径,而非App包内的实际路径,说明运行时未正确指向嵌入的Python核心库。

解决方案

1. 确保Python资源正确嵌入App包

  • 将Python-Apple-support编译得到的Python3.framework添加到Xcode项目,设置Embed & Sign,确保它被打包到App的Frameworks目录。
  • 复制Python标准库(包含encodings模块的lib/python3.9目录)到项目的Resources文件夹,勾选"Copy items if needed",确保这些文件被打包进App。

2. 修正环境变量设置逻辑

使用App包内的相对路径获取资源,替换原有代码:

func RunPythonScript() -> PythonObject {
    // 获取App内Python标准库的路径
    guard let pythonLibPath = Bundle.main.path(forResource: "python3.9", ofType: nil, inDirectory: "lib") else {
        fatalError("Python library not found in bundle")
    }
    let pythonHomePath = pythonLibPath.deletingLastPathComponent.deletingLastPathComponent // 指向Python框架根目录
    
    // 设置环境变量
    setenv("PYTHONHOME", pythonHomePath, 1)
    setenv("PYTHONPATH", pythonLibPath, 1)
    
    // 初始化Python并添加自定义脚本路径
    let sys = Python.import("sys")
    guard let scriptPath = Bundle.main.path(forResource: "", ofType: nil, inDirectory: "Scripts") else {
        fatalError("Script directory not found")
    }
    sys.path.append(scriptPath)
    
    let file = Python.import("pythonscript")
    let response = file.hello_world()
    print(response)
    return response
}

3. 验证路径配置

添加打印语句确认路径是否正确指向App内资源:

print("PYTHONHOME:", String(cString: getenv("PYTHONHOME")))
print("sys.path:", sys.path)

确保输出的路径是App包内的路径,而非编译时的runner路径。

4. 检查编译配置

  • 确认Python-Apple-support的编译目标平台(模拟器/真机)与Xcode项目部署目标匹配。
  • 检查编译生成的框架内是否存在lib/python3.9/encodings目录,确保核心模块未缺失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 04:55:22