Adding __init__.py not working - Module Not Found: Fixing Python Sibling Directory Import Error
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: https://www.linkedin.com/in/gustafrcavanaugh/
Adding __init__.py not working - Module Not Found: Fixing Python Sibling Directory Import Error
Relative Package Imports in Python
Importance of Modular Code
- The video discusses the significance of relative package imports in Python, emphasizing the need for good software engineering practices in machine learning to avoid "spaghetti code."
- The goal is to create modular code that enhances reusability and collaboration among developers, moving away from isolated coding practices.
Project Structure Overview
- An example project structure is presented, featuring a source directory named "Fu," which contains subdirectories for models and tests.
- This structure serves as a foundational layout for organizing model code and testing functionalities, although it is described as incomplete and illustrative.
Running Code and Initial Issues
- A demonstration shows a top-level run file designed for end users to execute the code without delving into individual packages.
- However, an error occurs when attempting to run a test file that imports from the models directory, indicating a module not found issue.
Understanding Absolute Imports
- Reference is made to Python documentation regarding absolute imports within packages, highlighting how they can be used effectively across sibling packages.
- An example illustrates importing modules using absolute paths (e.g.,
from sound.effects import Echo), which clarifies the intended approach for resolving import issues.
Solution with setup.py
- The speaker introduces
setup.pyas a solution to manage package imports more effectively by utilizing setuptools.