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

如何在karate.exec()中指定本地或Karate项目内Python脚本路径?

解决Karate中karate.exec()执行Python脚本的路径问题

针对本地系统中的Python脚本

直接使用绝对路径,注意不同操作系统的路径格式:

  • Windows系统:路径用正斜杠,或者转义反斜杠,带空格的路径要加引号
    # 正斜杠写法
    karate.exec('python "C:/Users/your-name/scripts/local-test.py"')
    # 转义反斜杠写法
    karate.exec('python "C:\\Users\\your-name\\scripts\\local-test.py"')
    
  • Linux/macOS系统:直接用绝对路径,Python3需指定python3
    karate.exec('python3 /home/your-name/scripts/local-test.py')
    

如果出现working directory: null,可以先手动设置工作目录:

karate.set('karate.working.dir', 'C:/Users/your-name/scripts') # Windows
# 或者
karate.set('karate.working.dir', '/home/your-name/scripts') # Linux/macOS

针对Karate项目内部的Python脚本

优先使用项目相对路径,默认工作目录是项目根目录,比如脚本放在src/test/resources/scripts/下:

karate.exec('python src/test/resources/scripts/inner-test.py')

如果担心路径偏差,用Karate的karate.resourcePath()获取资源文件的绝对路径:

def scriptAbsPath = karate.resourcePath('scripts/inner-test.py')
karate.exec('python ' + scriptAbsPath)

额外注意事项

  • 确认Python命令正确:系统中Python3可能需要用python3而非python
  • 路径含空格时必须给路径包裹双引号,避免解析错误
  • Linux/macOS下确保脚本有可执行权限:chmod +x /path/to/script.py

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 06:40:46