0

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

9
  • I would add a pytest.ini file and add a setting called pythonpath. This will let you point to the location of amapitest for the duration of your tests. Here's an example of the file from my python template repo. Commented Aug 16, 2022 at 14:28
  • You need an __init__.py File in all importable folders (so it’s missing in src). You shouldn’t need to modify your pythonpath if you’re executing this from the root directory. Commented Aug 16, 2022 at 14:35
  • @MichaelDelgado they are all here, in every folder, Ive just remove all "unnecessary" content for space economy in my question)) and now I guess it was a bad idea
    – Evgenii
    Commented Aug 16, 2022 at 14:44
  • Oh. Yeah don’t go so far as to remove things critical to the question at hand :) could you add those back in and also provide the path where you call pytest? And is there a conftest.py anywhere? Please provide those locations and the contents. Commented Aug 16, 2022 at 14:52
  • I think the problem might just be that you’re calling pytest from inside amapitest? Or are you in the enclosing dir? Commented Aug 16, 2022 at 14:53

3 Answers 3

0

Your current directory is automatically added to sys.path by locust, according to the documentation https://docs.locust.io/en/stable/writing-a-locustfile.html#how-to-structure-your-test-code

Try going to the parent directory and run

locust -f amapitest/my_locustfiles/instr_performance.py

1
  • I did it and even absolute path) it doesnt worked
    – Evgenii
    Commented Aug 18, 2022 at 14:25
0

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

2
  • Good that you managed to get it working, strange that you had to resort to that. If you ever want to dig deeper, you should probably try printing sys.path or something.
    – Cyberwiz
    Commented Aug 18, 2022 at 14:41
  • Perhaps you also need an __init__.py in amapitest
    – Cyberwiz
    Commented Aug 18, 2022 at 14:42
0
  1. print(sys.path) to see the path that helps Python to find modules.

  2. If the path is found. e.g. Let's say "/Users/user/SomeFolder/amapitest/", try @Cyberwiz's comment, adding __init__.py file in every folder to turn folders to modules. Execute locust at amapitest folder

If you want to execute locust at sub-folder and module not found still happened, try Step 3.

  1. Within instr_performance.py, append parent path like this
    sys.path.append(
        os.path.join(
            os.path.dirname(__file__), '..'
        )
    )
    
    You will see '/Users/user/SomeFolder/amapitest/my_locustfiles/..' when print(sys.path), I think this workaround may helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.