HttpRunner httprunner debugtalk.py 如何引用自定义模块

paolo · 2023年06月26日 · 最后由 tmac 回复于 2024年04月07日 · 4420 次阅读

我自己写了几个模块,想在 debugtalk.py 引用,使用 from xxx.bbb import *,
直接执行 debugtalk.py main 执行是可以的,
但是用命令行 hrp 运行脚本就直接报错了,像这种 debugtalk 引用自定义模块该如何操作?

9:32PM INF ensure python3 venv packages=["funppy==v0.5.0"] python3="C:\Users\xxxx\.hrp\venv\Scripts\python.exe"
9:32PM INF python package is ready name=funppy version=v0.5.0
9:32PM INF set python3 executable path Python3Executable="C:\Users\xxxx\.hrp\venv\Scripts\python.exe"
2023-06-26T21:32:04.054+0800 [WARN] grpc-py: plugin configured with a nil SecureConfig
9:32PM ERR init plugin failed: D:\code\hrpdemo.debugtalk_gen.py error="connect grpc plugin failed: Unrecognized remote plugin message: \n\nThis usually means that the plugin is either invalid or simply\nneeds to be recompiled to support the latest protocol."
9:32PM ERR [Run] init case runner failed error="init plugin failed: connect grpc plugin failed: Unrecognized remote plugin message: \n\nThis usually means that the plugin is either invalid or simply\nneeds to be recompiled to support the latest protocol.: init plugin failed"
2023-06-26T21:32:04.161+0800 [ERROR] grpc-py: plugin process exited: path=C:\Users\xxxxx.hrp\venv\Scripts\python.exe pid=23628 error="exit status 1"
Error: init plugin failed: connect grpc plugin failed: Unrecognized remote plugin message:

This usually means that the plugin is either invalid or simply
needs to be recompiled to support the latest protocol.: init plugin failed
9:32PM WRN hrp exit code=31

共收到 3 条回复 时间 点赞

问题解决了, 咨询 openai 告诉我的答案如下:
在 httprunner 的 debugtalk.py 文件中,可以通过以下两种方式引用自定义模块:

  1. 将自定义模块的路径添加到 PYTHONPATH 中,然后在 debugtalk.py 中使用 import 语句引入模块,例如: python import sys import os sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(file), ".."))) from my_module import my_function
  2. 直接把自定义模块的代码复制到 debugtalk.py 中,然后在 debugtalk.py 中直接调用自定义模块中的函数,例如: python def my_function(): # 自定义模块中的函数代码 pass

我实际碰到的问题是自定义模块又有 import 第三方库,当时在 C:\Users\xxxxx.hrp\venv 虚拟环境中漏掉 install 其中一个模块,实际是这里报错了,但是 hrp 显示的错误让人迷惑。

运行的时候会创建一个虚拟环境,这个环境跟系统环境隔离,如果你在 debugtalk.py 引用第三方包,找不到就会报你说的错误。只需要改动 pyvenv.cfg 文件中的 include-system-site-packages 为 true 即可

还有其他的解决办法么,尝试了好多都没解决

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册