relative imports sst test cases

Asked by mr17 on 2013-06-17

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

Corey Goldberg (coreygoldberg) said : #2

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

mr17 (marsel) said : #3

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