Adding __init__.py not working - Module Not Found: Fixing Python Sibling Directory Import Error

Published: 11 December 2022
on channel: Gus Cavanaugh
31,360
522

I've been trying to use better project structure for my ML demos, but I keep getting punched in the face by Python when importing sibling packages.

my_ml_project/
run_project.py
sweet_src_code/
models_dir/
amazing_model.py
tests_dir/
beautiful_test.py

I add __init__.py files everywhere, re-read the python docs (https://lnkd.in/gcgeb6qA), and remain utterly confused.

I then usually give up and put everything in the top level dir:

my_ml_project/
run_project.py
amazing_model.py
beautiful_test.py


I finally came across a stack overflow answer that I had previously missed: use a setup.py file.

This video is a quick demonstration of what doesn't work (failing to import my model_dir from a sibling test_dir) and how adding a short setup.py file to the project root fixes the problem.

This is basic stuff but I now feel such relief. No more pentagrams of salt, ritualistic animal sacrifices, and __init__.py files everywhere trying to get imports to work.

If you want to connect with me, I'm most active on LI. Feel free to connect with me:   / gustafrcavanaugh  


Watch video Adding __init__.py not working - Module Not Found: Fixing Python Sibling Directory Import Error online without registration, duration hours minute second in high quality. This video was added by user Gus Cavanaugh 11 December 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 31,360 once and liked it 522 people.