relative imports sst test cases

Asked by mr17

I am new to python and sst and this is more of a python related question so probably I should not be posting here.
Anyway, I have my sst test organized like this :

/sst_tests
__init__.py
                 /shared
                      __init__.py
                       utils.py
                       userpage.py
                 /test_case
                       __init__.py
                      test1.py
                      test2.py

test1.py will have import
from ..shared import utils

sst-run test1
ValueError: Attempted relative import in non-package

Am I missing something here, it seem I can not get this to work ?

thanks,
Marsel

Question information

Language:
English Edit question
Status:
Solved
For:
selenium-simple-test Edit question
Assignee:
No assignee Edit question
Solved by:
mr17
Solved:
Last query:
Last reply:
Revision history for this message
Vincent Ladeuil (vila) said :
#1

While sst scripts doesn't require any import path, sharing code between scripts requires a well defined import space. python modules you need to import should be available from a single entry in sys.path (or PYTHONPATH if you need to control it from the command line). So relative imports should generally be avoided.

There are several ways to achieve what you want all boiling down to allowing you to import from the shared directory:

- add shared in PYTHONPATH (you get full control about what can be imported),
- specify --shared 'shared' when running sst-run (which will add shared to sys.path for you and won't try to discover tests in 'shared')
- define a discover method in shared/__init__.py (from sst import loader ; discover = loader.discoverNoTests

The later will ensure that sst will never try to discover test scripts in the shared directory and let you add it to your PYTHONPATH, i.e. you get full control.

And instead of 'from ..shared import utils' you just do 'import utils'.

Revision history for this message
Corey Goldberg (coreygoldberg) said :
#2

+1 to what vincent said :)
btw, this would make a great FAQ/doc entry

Revision history for this message
mr17 (marsel) said :
#3

Thank you Vincent, adding /shared to PYTHONPATH did what I wanted .