我的机器人项目看起来像这样:
lib/
__init__.py
utils.py
foo.py
__init__.robot
sample.robot
我将一些util函数写入utils.py
并在foo.py
中使用它。但是,当我使用robot
运行sample.robot
时,会抛出异常:
[ ERROR ] Error in file '/Users/tuanchauict/Desktop/demo/sample.robot': Importing test library '/Users/tuanchauict/Desktop/demo/lib/foo.py' failed: ImportError: attempted relative import with no known parent package
Traceback (most recent call last):
File "/Users/tuanchauict/Desktop/demo/lib/foo.py", line 1, in <module>
from .lib.utils import *
PYTHONPATH:
/Users/tuanchauict/.local/share/virtualenvs/demo-XFo70VU1/bin
/Users/tuanchauict/.local/share/virtualenvs/demo-XFo70VU1/lib/python36.zip
/Users/tuanchauict/.local/share/virtualenvs/demo-XFo70VU1/lib/python3.6
/Users/tuanchauict/.local/share/virtualenvs/demo-XFo70VU1/lib/python3.6/lib-dynload
/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6
/Users/tuanchauict/.local/share/virtualenvs/demo-XFo70VU1/lib/python3.6/site-packages
==============================================================================
Sample
==============================================================================
Demo | FAIL |
No keyword with name 'Test Keyword' found.
------------------------------------------------------------------------------
Sample | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
Output: /Users/tuanchauict/Desktop/demo/output.xml
Log: /Users/tuanchauict/Desktop/demo/log.html
Report: /Users/tuanchauict/Desktop/demo/report.html
文件内容:
utils.py
def foo():
pass
foo.py
from .lib.utils import * # I also tried `from .utils import *`
def test_keyword():
pass
sample.robot
*** Settings ***
Library lib/foo.py
*** Test Cases ***
Demo
Test Keyword
我使用Python3.6运行