[Tutor] Imports (Py 3)
Alex Kleider
akleider at sonic.net
Mon Jan 13 16:59:42 EST 2020
On 2020-01-13 06:49, Mats Wichmann wrote:
> On 1/13/20 6:33 AM, S D wrote:
>> I have a folder `abc_app`, which contains a module `utils`. In the
>> `abc_app` folder, I also have a subfolder `tests` which contains a
>> module
>> `test_api`
>>
>> How do I import a function from the `utils` module from the `test_api`
>> module?
>
> "relative imports". which can be vaguely confusing, it's worth
> reading a tutorial on them to be sure you understand.
>
> if "foo" is in "utils", what you described ought to look like:
>
> from ..utils import foo
>
> (unless I managed to confuse myself!)
>
This is a problem with which I also have struggled and was never able to
get the (..) syntax of relative imports to work so I solved it another
way placing the following code at the top of the file containing the
tests:
path2add = os.path.expandvars("${CLUB}/Utils")
sys.path.append(path2add)
If the first line confuses you just ignore it and assign the full path
name to and including your 'utils.py' file to the path2add variable.
More information about the Tutor
mailing list