question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Import issue when the locustfile.py contains importing self-defined class sentence

See original GitHub issue

Hi, I see some import issue when I tried to run load test with the locust command line. Here is my folder hierachy.

root/ folder1/ pythonClass1.py folder2/ pythonClass2.py folder3/ loadTest.py

The pythonClass1.py and pythonClass2.py is normal python class file including Class and some functions. The loadTest.py file is just like locustfile.py file in the official website

In my load test file loadTest.py, I hope I could reuse the code in pythonClass1 so I try to import pythonClass1 with the following import sentence:

          from ..folder1.pythonClass1 import PythonClass1

when I run the locust command in the root directory

          locust -f folder3/loadTest.py

errors occured

It throws out “Attempted relative import in non-package” and the error like below.

I used the same import sentence for my other simple tests, not load test. it works fine. When I run my simple tests, I just use “python -m …” then no such import error occured. How can I solve this problem when I run “locust -f …”?

[2016-08-25 15:03:40,180] Unknown/ERROR/stderr: Traceback (most recent call last): [2016-08-25 15:03:40,180] Unknown/ERROR/stderr: File “C:\Python27\Scripts\locust-script.py”, line 9, in <module> [2016-08-25 15:03:40,180] Unknown/ERROR/stderr: [2016-08-25 15:03:40,180] Unknown/ERROR/stderr: load_entry_point(‘locustio==0.7.5’, ‘console_scripts’, ‘locust’)() [2016-08-25 15:03:40,180] Unknown/ERROR/stderr: File “C:\Python27\lib\site-packages\locust\main.py”, line 349, in main [2016-08-25 15:03:40,181] Unknown/ERROR/stderr: [2016-08-25 15:03:40,181] Unknown/ERROR/stderr: docstring, locusts = load_locustfile(locustfile) [2016-08-25 15:03:40,181] Unknown/ERROR/stderr: File “C:\Python27\lib\site-packages\locust\main.py”, line 321, in load_locustfile [2016-08-25 15:03:40,183] Unknown/ERROR/stderr: [2016-08-25 15:03:40,183] Unknown/ERROR/stderr: imported = import(os.path.splitext(locustfile)[0]) [2016-08-25 15:03:40,183] Unknown/ERROR/stderr: File “C:\Python27\lib\site-packages\gevent\builtins.py”, line 93, in import [2016-08-25 15:03:40,183] Unknown/ERROR/stderr: [2016-08-25 15:03:40,183] Unknown/ERROR/stderr: result = _import(_args, *_kwargs) [2016-08-25 15:03:40,184] Unknown/ERROR/stderr: File “c:\CodeSpace\TestScripts\apitest\src\test\python\testscripts\load\ImportIssue.py”, line 7, in <module> [2016-08-25 15:03:40,184] Unknown/ERROR/stderr: [2016-08-25 15:03:40,184] Unknown/ERROR/stderr: from …folder1.pythonClass1 import PythonClass1 [2016-08-25 15:03:40,184] Unknown/ERROR/stderr: File “C:\Python27\lib\site-packages\gevent\builtins.py”, line 93, in import [2016-08-25 15:03:40,184] Unknown/ERROR/stderr: [2016-08-25 15:03:40,184] Unknown/ERROR/stderr: result = _import(_args, *_kwargs) [2016-08-25 15:03:40,184] Unknown/ERROR/stderr: ValueError [2016-08-25 15:03:40,184] Unknown/ERROR/stderr: : [2016-08-25 15:03:40,184] Unknown/ERROR/stderr: Attempted relative import in non-package [2016-08-25 15:03:40,184] Unknown/ERROR/stderr:

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
v-devcommented, Aug 30, 2016

Ah ok thanks. I already had an __init__.py in my “package” directories? but didn’t know I had to run using python -m locust.main -f /path/to/locustfile.py... instead of locust -f /path/to/locustfile.py... as was provided in the quickstart documentation.

2reactions
Callie-C-Wangcommented, Aug 30, 2016

I think the error is due to my misunderstanding of module concept. The problem can be solved in this way.

  1. in code level create a --init–.py file for each folder. (–init–.py means _ _ init _ _.py without any space between _ ) this mean to tell python that this folder is a module. then put some import sentence in this --init–.py file, this means this module is telling everybody else that: I have this class in my module.

e.g. Below is the folder hierachy root/ root/folder1/ root/folder1/pythonClass1.py root/folder1/ --init–.py root/folder2/ root/folder2/pythonClass2.py root/folder2/ --init–.py root/folder3/ root/folder3/loadTest.py

put “from pythonClass1 import CLASSA” in root/folder1/ --init–.py put “from pythonClass2 import CLASSB” in root/folder2/ --init–.py

2)in command line level you have to launch the locust test in some folder which is above both folder 3 and folder 2 and folder 1. And the import sentence in loadTest.py is depending on what directory you are in when you run the locust command.

E.g. we are in root/ directory. Then the import sentence in loadTest.py should be like from folder1.pythonClass1 import CLASSA from folder2.pythonClass2 import CLASSB

And you should launch the locust in the following command(still in root/ directory): root> python -m locust.main -f folder3/loadTest.py

then it should work.

loadTest.py is a file similar like locustfile.py in the official site example.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing a locustfile — Locust 2.14.0 documentation
A locust file is just a normal Python module, it can import code from other files ... For a file to be a...
Read more >
How to run locust with multiple test files - python - Stack Overflow
Write tests in different files. Ensure the classes in each of these files are named differently. import the classes from all different files ......
Read more >
Python Locust Module - Javatpoint
We have shown you one method for testing non-RESTful APIs. There are different instruments like JMeter for load testing. JMeter has a high...
Read more >
latest PDF - BentoML
BentoML requires Python 3.7 or above. pip install bentoml. To install all additional features in BentoML, such as gRPC, S3 support, and more, ......
Read more >
Source - GitHub
... HTTPLocust [\#466](https://github.com/locustio/locust/issues/466) - Import issue when the locustfile.py contains importing self-defined class sentence ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found