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 :

             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 ?


Question information

English Edit question
selenium-simple-test Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
Revision history for this message
Vincent Ladeuil (vila) said :

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/ (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 :

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

Revision history for this message
mr17 (marsel) said :

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