Asking this question here because I've searched for the answer more than a few times and have yet to properly resolve it.
Let's say that I have a program folder structure that looks like this:
src/
tests/
test1.py
code/
__init__.py
moduleA.py
moduleB.py
and moduleA.py
contains a function my_function(x)
that I'd like to call from test1.py
. __init__.py
is an empty file.
What is the appropriate way to access this function?
So far I've tried:
# In test1.py
from code import moduleA
moduleA.my_function(1)
Which results in the following error:
ModuleNotFoundError: No module named 'code'
# in test1.py
from .code import moduleA # or: "from ..code import moduleA"
moduleA.my_function(1)
But this results in the following error:
ImportError: attempted relative import with no known parent package
I'd really like to avoid modifying the path or adding extra function calls besides these import
lines. Is there a good reason that I'm unable to step through the folder tree using .
as appropriate?
Thank you!