Home > Enterprise >  ModuleNotFoundError : No module named with locust
ModuleNotFoundError : No module named with locust

Time:09-23

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 _Global.py file

FILE : getWalletInfoLocust.py

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

class ApiUser(HttpUser):

    def __init__(self, environment):
        print("__init__"   generateIlogsToken())
        super().__init__(environment)


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

Error generated with the command locust -f getWalletInfoLocust.py

...
    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.

Thx

CodePudding user response:

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:

project_directory
│   getWalletInfoLocust.py  
│   ... 
└───Phoenix 
│   │   ...   
│   └───APIs   
│   │   │   _Global.py
│   │   │   ...

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 getWalletInfoLocust.py from the directory that I called "project_directory" in my example, otherwise python could not find the module Phoenix.

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

import sys, os
sys.path.append('path/to/project_directory')

CodePudding user response:

Your Phoenix library is a folder not a package. So it needs to be imported following this https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly or this Importing files from different folder.

Hope it helps.

  • Related