Search code examples
pythonpytestperformance-testinglocustweb-api-testing

Pytest/Locust: ModuleNotFoundError No module named


Ive tried to find anwsers on similar topics, but... nothing helped.
When I run my regular tests with pytest -m blablabla - there are no problems, but
when I run locust by command:

locust -f my_locustfiles/instr_performance.py

than got this:

(venv) evgen@TLL amapitest % locust -f my_locustfiles/instr_performance.py
Traceback (most recent call last):
  File "/Users/evgen/venv/bin/locust", line 8, in <module>
    sys.exit(main())
  File "/Users/evgen/venv/lib/python3.10/site-packages/locust/main.py", line 70, in main
    docstring, _user_classes, shape_class = load_locustfile(_locustfile)
  File "/Users/evgen/venv/lib/python3.10/site-packages/locust/util/load_locustfile.py", line 58, in load_locustfile
    imported = source.load_module()
  File "<frozen importlib._bootstrap_external>", line 548, in _check_name_wrapper
  File "<frozen importlib._bootstrap_external>", line 1063, in load_module
  File "<frozen importlib._bootstrap_external>", line 888, in load_module
  File "<frozen importlib._bootstrap>", line 290, in _load_module_shim
  File "<frozen importlib._bootstrap>", line 719, in _load
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/evgen/PycharmProjects/api-testing/amapitest/my_locustfiles/instr_performance.py", line 8, in <module>
    from amapitest.src.helpers.jwt_generator import generate_json_web_token
ModuleNotFoundError: No module named 'amapitest.src.helpers'

my project structure:

├── amapitest  
│   ├── my_locustfiles  
│   │   ├── __init__.py  
│   │   └── instr_performance.py  
│   ├── src  
│   │   ├── configs  
│   │   ├── dao  
│   │   ├── helpers  
│   │   └── utilities  
│   ├── tests  
│   └── tmp  
├── amapitest.egg-info  
├── build  
├── dist  
├── env.sh  
├── requirements.txt  
└── setup.py  

additional info:
locust 2.11.0
pytest 7.1.2
python 3.10
macOS monterey


Solution

  • Ive solved my problem by moving "my_locustfiles" folder into root directory of project and running test from this directory.

    I still dont understand why locust cant find imported methods when locust file was located in "amapitest"

    but...anyway, thanks everyone