Import issue when the locustfile.py contains importing self-defined class sentence
See original GitHub issueHi, 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:
- Created 7 years ago
- Comments:9 (1 by maintainers)
Top GitHub Comments
Ah ok thanks. I already had an
__init__.py
in my “package” directories? but didn’t know I had to run usingpython -m locust.main -f /path/to/locustfile.py...
instead oflocust -f /path/to/locustfile.py...
as was provided in the quickstart documentation.I think the error is due to my misunderstanding of module concept. The problem can be solved in this way.
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.