ModuleNotFoundError : No module named with locust

When I try to import external library of my project in my locust python file, each time I have the error "ModuleNotFoundError : No module name ‘…’.

Apparently is not clear but locust is installed and works.
I have a task to make REST API call and it’s ok.
But I need to make some action in the init method coming from external file of my project

Example :

I want to import the method "generateILogsToken" in my locust file from file


from Phoenix.APIs._Global import generateIlogsToken
from locust import HttpUser, task

class ApiUser(HttpUser):

    def __init__(self, environment):
        print("__init__" + generateIlogsToken())

    def profile(self):
        url = "/restapi/json/v1/resources"
        self.client.get(url, verify=False, headers={'AUTHTOKEN': 'xxxxx'})

Error generated with the command locust -f

    from Phoenix.APIs._Global import generateIlogsToken
ModuleNotFoundError: No module named 'Phoenix'

I have this issue for any method imported from external file of my project.
When I’m used behave or other, no isse. It’s only with locust.


>Solution :

I am not sure, but I think the problem could be related to the fact that you launch your program in a folder that is not the root folder of your project.

Let’s say you have a directory tree like this:

│   ... 
│   │   ...   
│   └───APIs   
│   │   │
│   │   │   ...

When launching the command, python will look for the Phoenix folder from the folder you are launching python from.

To be sure the module is found, you should launch locust -f from the directory that I called "project_directory" in my example, otherwise python could not find the module Phoenix.

Alternatively you can add Phoenix directory to sys.path in your file, like this:

import sys, os

Leave a Reply