I have been attmepting to solve this for months now, and for the life of me cannot figure it out. Every solution I have tried from stack overflow has failed me so far :(
The problem is simple. Let say I have a module, utils, that contains some function. Now, lets also say I have a tests folder where I want to setup a unit test for that function.
Project
|
+— venv
|
+— .gitignore
|
+— requirements.txt
|
+— main.py
|
+— utils/
| +— __init__.py
| +— test_function.py
+— tests/
| +— test.py
From sample projects I have seen on github, this is very simple to import. You would just say:
from utils.test_function import some_function
However, when I attempt this, I get the error
ModuleNotFoundError: No module named utils
From previous searches, I have found a temporary solution of appending the module to my path.
sys.path.append('absolute_path/utils')
import some_function
However, this seems like a terrible solution considering I need an absolute path. Furthermore, this does not seem the best practice based on every github project I have seen. Sample repo that demonstrates this. However, I cannot figure out how to do this correctly. Thank you for the help.
Edit: Please note, I am not asking the exact same questions as this: Running unittest with typical test directory structure
Another example I have is when I am running jupyter notebook in a notebooks/ folder, and want to import a function from another module in my project. Same issue as the original question.