pwnage101 · GitHub

I maintain multiple load tests, each with their own locustfile. They each
share helper utilities located at the project root, but locust/main.py loads
the locustfile in a way that does not make importing the helpers easy. I have
to add the following line to every locustfile to make sure the helper utilities
are importable:

I think it's understandable why locust/main.py inserts the locustfile
directory into the sys.path before loading it. I also think it would be
harmless to also make sure the current working directory is in the sys.path.

$ find
.
./__init__.py
./helpers
./helpers/__init__.py
./loadtests
./loadtests/__init__.py
./loadtests/test1
./loadtests/test1/locustfile.py
./loadtests/test1/__init__.py
./loadtests/test2
./loadtests/test2/locustfile.py
./loadtests/test2/__init__.py

Before this change, the common helpers module could not be found.

$ locust -f loadtests/test1
[2016-09-27 14:00:48,712] carbon/ERROR/stderr: Traceback (most recent call last):
[2016-09-27 14:00:48,712] carbon/ERROR/stderr: File "/home/tsankey/Documents/perf-357/locust_minimal_example/venv/bin/locust", line 9, in <module>
[2016-09-27 14:00:48,712] carbon/ERROR/stderr:
[2016-09-27 14:00:48,712] carbon/ERROR/stderr: load_entry_point('locustio==0.7.3', 'console_scripts', 'locust')()
[2016-09-27 14:00:48,712] carbon/ERROR/stderr: File "/home/tsankey/Documents/perf-357/locust_minimal_example/venv/local/lib/python2.7/site-packages/locustio-0.7.3-py2.7.egg/locust/main.py", line 349, in main
[2016-09-27 14:00:48,712] carbon/ERROR/stderr:
[2016-09-27 14:00:48,712] carbon/ERROR/stderr: docstring, locusts = load_locustfile(locustfile)
[2016-09-27 14:00:48,712] carbon/ERROR/stderr: File "/home/tsankey/Documents/perf-357/locust_minimal_example/venv/local/lib/python2.7/site-packages/locustio-0.7.3-py2.7.egg/locust/main.py", line 321, in load_locustfile
[2016-09-27 14:00:48,713] carbon/ERROR/stderr:
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: imported = __import__(os.path.splitext(locustfile)[0])
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: File "/home/tsankey/Documents/perf-357/locust_minimal_example/venv/local/lib/python2.7/site-packages/gevent-1.1.1-py2.7-linux-x86_64.egg/gevent/builtins.py", line 93, in __import__
[2016-09-27 14:00:48,713] carbon/ERROR/stderr:
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: result = _import(*args, **kwargs)
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: File "/home/tsankey/Documents/perf-357/locust_minimal_example/example/loadtests/test1/__init__.py", line 1, in <module>
[2016-09-27 14:00:48,713] carbon/ERROR/stderr:
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: from locustfile import MyLocustClass
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: File "/home/tsankey/Documents/perf-357/locust_minimal_example/venv/local/lib/python2.7/site-packages/gevent-1.1.1-py2.7-linux-x86_64.egg/gevent/builtins.py", line 93, in __import__
[2016-09-27 14:00:48,713] carbon/ERROR/stderr:
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: result = _import(*args, **kwargs)
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: File "/home/tsankey/Documents/perf-357/locust_minimal_example/example/loadtests/test1/locustfile.py", line 10, in <module>
[2016-09-27 14:00:48,713] carbon/ERROR/stderr:
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: import helpers
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: File "/home/tsankey/Documents/perf-357/locust_minimal_example/venv/local/lib/python2.7/site-packages/gevent-1.1.1-py2.7-linux-x86_64.egg/gevent/builtins.py", line 93, in __import__
[2016-09-27 14:00:48,713] carbon/ERROR/stderr:
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: result = _import(*args, **kwargs)
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: ImportError
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: :
[2016-09-27 14:00:48,713] carbon/ERROR/stderr: No module named helpers
[2016-09-27 14:00:48,713] carbon/ERROR/stderr:
$ locust -f loadtests/test1
[2016-09-27 13:59:24,798] carbon/INFO/stdout: helpers imported!
[2016-09-27 13:59:24,798] carbon/INFO/stdout:
[2016-09-27 13:59:24,798] carbon/INFO/locust.main: Starting web monitor at *:8089
[2016-09-27 13:59:24,799] carbon/INFO/locust.main: Starting Locust 0.7.3

Read the original on github.com ↗