python imports are weird in the sense that you can't just do
Depending on your file structure, things can get a bit complicated. Here's my fix.
file structure: Project_Parent/ ├── moduleA.py └── Application/ | ├── app.py | └── Module/ | ├── __init__.py | └── moduleA.py └── Tests/ ├── __init__.py └── test_moduleA.py
In this scenario,
Tests is my unit test directory and I want
test_moduleA.py to import
Application directory is the parent of the project so we need to add it to
sys.path for python to be able to find it.
import sys sys.path.insert(0, os.path.join('Project_Parent', 'Application')) from Module.moduleA import function_in_moduleA
Got comments or issues with what I wrote? Please let me know by submitting an issue with the blog title :)