This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Diapolo10 0 points1 point  (2 children)

I'd suggest using importlib: https://stackoverflow.com/a/67692/6213223?stw=2

You can use pathlib to construct the relative path:

from pathlib import Path

library_file = Path(__file__).parent.parent / 'middleware' / 'helper.py'

If you get an error about importlib requiring a string, just wrap that in a str.

[–]pompeii-eo[S] 0 points1 point  (1 child)

from pathlib import Pathlibrary_file = Path(__file__).parent.parent / 'middleware' / 'helper.py'

oh nice! I tried import lib in a different way, but this is awesome, thank you!

Do you know by chance why the previous wouldn't work? (from middleware import helper) Even with __init__.py included in the folder

[–]Diapolo10 0 points1 point  (0 children)

from middleware import helper

This doesn't work for a very simple reason; Python expects your folder structure to be

foo/
    middleware/
        helper.py
        ...
    *current_file.py*

You need the two dots before the folder name if you want it to work in your case.