【问题标题】:Cannot import name 'User' from 'Locust' library无法从“蝗虫”库中导入名称“用户”
【发布时间】:2020-05-07 01:13:24
【问题描述】:

我对@9​​87654322@ 还很陌生,几天前才刚刚开始修补它。

以前我是按照别人的例子使用的:

class WebsiteUser(HttpLocust):
   # Do stuff

但是在阅读了更多文档后,我尝试使用 User 类,现在当我尝试导入所述类时出现错误:

我只是复制/粘贴了docs 中的示例,以确保没有任何拼写错误:

from locust import User, TaskSet, task, between

class MyTaskSet(TaskSet):
    @task
    def my_task(self):
        print("executing my_task")

class MyUser(User):
    tasks = [MyTaskSet]
    wait_time = between(5, 15)

回复:

local_1          | [2020-05-07 00:54:07,494] c8343450b35a/ERROR/stderr: File "/src/locustfile.py", line 153, in <module>
local_1          | [2020-05-07 00:54:07,495] c8343450b35a/ERROR/stderr: 
local_1          | [2020-05-07 00:54:07,495] c8343450b35a/ERROR/stderr: from locust import User, TaskSet, task, between
local_1          | [2020-05-07 00:54:07,495] c8343450b35a/ERROR/stderr: 
local_1          | [2020-05-07 00:54:07,495] c8343450b35a/ERROR/stderr: ImportError
local_1          | [2020-05-07 00:54:07,495] c8343450b35a/ERROR/stderr: :
local_1          | [2020-05-07 00:54:07,495] c8343450b35a/ERROR/stderr: cannot import name 'User'

注意:我在 docker 容器中运行 locust。如果我切换回使用:

class WebsiteUser(HttpLocust):
    task_set = UserBehaviour # class UserBehaviour(TaskSet):
    wait_time = between(5.0, 9.0)

一切正常。

这是我正在使用的requirements.txt

locustio
influxdb==5.3.0

我正在使用最新版本的locust docker image locustio/locust:0.14.6 所以一切都应该是最新的。

  1. 您认为这是文档的问题,应该更新还是我遗漏了什么,这是我的错?
  2. 我还注意到我可以导入一个Locust 类,它的行为方式似乎与User 类相同,但我没有看到任何文档会提到Locust 类。 ..,有人知道吗?

【问题讨论】:

    标签: locust


    【解决方案1】:

    https://docs.locust.io/en/latest/ 上的文档指的是 locust 1.0(目前可直接从 github 获得并作为预发布包提供),其中所有 Locust 类已重命名为 User

    当前的“稳定”包 (0.14.6) 记录在这里:https://docs.locust.io/en/stable/

    希望这能解释你的情况。

    有关更改的更多信息,请参阅更改日志https://docs.locust.io/en/latest/changelog.html

    【讨论】:

    • 哇,愚蠢的错误……我看错了文档。谢谢,你实际上为我解决了不止一个问题!!!
    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多