Skip to content
Advertisement

Python cannot import from parent directory despite having made package

I am fairly new to Python, and despite searching all over and following many guides on this site, I am still stuck with a strange issue.

Folder structure is as follows

JavaScript

I am trying to simply import something from app.py into test_this.py

I have tried a few different things so far,
from app import func – This obviously won’t work as it is not in the same dir
So I tried:
from ..app import func and from . app import func This gives the following error:

JavaScript

Clearly I am missing or not understanding something here. I thought that I would be able to import it as I have that __init__.py file in the directory I am trying to import from.
Any chance someone could clarify this for me?

Advertisement

Answer

So the trick for me was to add the files I needed to the sys.path variable.

JavaScript

This allowed me to reference a file in a parent or sibling folder without errors.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement