cannot import name 'HttpUser' from 'locust' error when running Locust in Kubernetes

10/7/2020

I am deploying Locust on Kubernetes using Helm Charts(https://github.com/helm/charts/tree/master/stable/locust)

2 Worker pods are running with no errors, but master is in CrashLoopBackOff status.

When I check the logs for the failing pod I see following error:

[2020-10-07 21:46:18,300] locust-master-5bf8d86867-kfxxg/ERROR/stderr: Traceback (most recent call last):
[2020-10-07 21:46:18,300] locust-master-5bf8d86867-kfxxg/ERROR/stderr: File "/usr/local/bin/locust", line 11, in <module>
[2020-10-07 21:46:18,300] locust-master-5bf8d86867-kfxxg/ERROR/stderr: 
[2020-10-07 21:46:18,300] locust-master-5bf8d86867-kfxxg/ERROR/stderr: sys.exit(main())
[2020-10-07 21:46:18,300] locust-master-5bf8d86867-kfxxg/ERROR/stderr: 
[2020-10-07 21:46:18,301] locust-master-5bf8d86867-kfxxg/ERROR/stderr: File "/usr/local/lib/python3.7/site-packages/locust/main.py", line 391, in main
[2020-10-07 21:46:18,301] locust-master-5bf8d86867-kfxxg/ERROR/stderr: 
[2020-10-07 21:46:18,301] locust-master-5bf8d86867-kfxxg/ERROR/stderr: docstring, locusts = load_locustfile(locustfile)
[2020-10-07 21:46:18,301] locust-master-5bf8d86867-kfxxg/ERROR/stderr: 
[2020-10-07 21:46:18,301] locust-master-5bf8d86867-kfxxg/ERROR/stderr: File "/usr/local/lib/python3.7/site-packages/locust/main.py", line 358, in load_locustfile
[2020-10-07 21:46:18,301] locust-master-5bf8d86867-kfxxg/ERROR/stderr: 
[2020-10-07 21:46:18,302] locust-master-5bf8d86867-kfxxg/ERROR/stderr: imported = __import__(os.path.splitext(locustfile)[0])
[2020-10-07 21:46:18,302] locust-master-5bf8d86867-kfxxg/ERROR/stderr: 
[2020-10-07 21:46:18,302] locust-master-5bf8d86867-kfxxg/ERROR/stderr: File "/locust-tasks/tasks.py", line 1, in <module>
[2020-10-07 21:46:18,302] locust-master-5bf8d86867-kfxxg/ERROR/stderr: 
[2020-10-07 21:46:18,302] locust-master-5bf8d86867-kfxxg/ERROR/stderr: from locust import HttpUser, task, TaskSet, between
[2020-10-07 21:46:18,302] locust-master-5bf8d86867-kfxxg/ERROR/stderr: 
[2020-10-07 21:46:18,302] locust-master-5bf8d86867-kfxxg/ERROR/stderr: ImportError
[2020-10-07 21:46:18,302] locust-master-5bf8d86867-kfxxg/ERROR/stderr: :
[2020-10-07 21:46:18,302] locust-master-5bf8d86867-kfxxg/ERROR/stderr: cannot import name 'HttpUser' from 'locust' (/usr/local/lib/python3.7/site-packages/locust/__init__.py)
[2020-10-07 21:46:18,303] locust-master-5bf8d86867-kfxxg/ERROR/stderr:

I will appreciate if someone could help me understand what I am doing wrong. My thought was that I am using HttpUser and in the image that I am pulling (greenbirdit/locust) the is still LocustUser, but I don't know how to fix it.

Thank you for help!

-- Galia Vasylieva
kubernetes
locust

1 Answer

10/7/2020

Your link to Locust Helm Chart says it's using Locust container 0.9.0. Before Locust 1.0 HttpUser was HttpLocust. You can see the documentation for 0.9.0 here: https://docs.locust.io/en/0.9.0/

I don't use Helm, but it sure looks like you'll want to rewrite your locustfile to use HttpLocust and other pre-1.0 APIs until Helm updates the chart to use 1.0+.

-- Solowalker
Source: StackOverflow