Skip to content

Add missing __init__.py files to MegaDetector and megadetector directories#168

Closed
PetervanLunteren wants to merge 2 commits intoagentmorris:mainfrom
PetervanLunteren:main
Closed

Add missing __init__.py files to MegaDetector and megadetector directories#168
PetervanLunteren wants to merge 2 commits intoagentmorris:mainfrom
PetervanLunteren:main

Conversation

@PetervanLunteren
Copy link
Contributor

My AI friends advised me to add these __init__.py files so that I can properly import the modules in the detection dir. I had some really weird behaviour in my AddaxAI streamlit app. It seems to be resolved with these two extra empty files.

I asked ChatGPT to write some explanation:

This PR adds two missing __init__.py files:

  • MegaDetector/__init__.py
  • MegaDetector/megadetector/__init__.py

The MegaDetector/megadetector/detection/__init__.py file already exists.

Adding these files ensures that Python treats these directories as proper packages, enabling imports like:

from MegaDetector.megadetector.detection.video_utils import VIDEO_EXTENSIONS

to work reliably across different environments and tools (e.g., Streamlit, pytest, PyInstaller).

Without these files, Python can fail to locate modules properly, leading to errors such as KeyError or ImportError.

This change improves import consistency and overall package stability.

@agentmorris
Copy link
Owner

agentmorris commented Jul 13, 2025

The top-level "MegaDetector" folder (i.e., the root of the repo) is not a package, so this:

from MegaDetector.megadetector.detection.video_utils import VIDEO_EXTENSIONS

...is not a good idea. You should always be doing this:

from megadetector.detection.video_utils import VIDEO_EXTENSIONS

This will work if you've installed the megadetector Python package (or the megadetector-utils Python package), or if the repo root is on your PYTHONPATH. Lots of other problems will come up later if you try to treat the repo root as a package; it's not testable, it includes non-Python stuff, it breaks the way megadetector modules import each other, etc. The "megadetector" folder is the root of the package. This is a pretty typical package structure, where one folder in the repo is the root from which a Python package is built, and stuff like docs live outside the package root.

So, I don't think we want to add an __init__.py file to the repo root.

Adding an __init__.py file to the "megadetector" folder should be fine, although it shouldn't be necessary. I'll leave this PR open, but can we start by having you address any issues on your side that would have required this:

from MegaDetector.megadetector.detection.video_utils import VIDEO_EXTENSIONS

...which I promise you don't want to do, so I think that's the main issue to address to avoid problems later. Then let's come back here and see whether you still need an __init__.py file in the "megadetector" folder?

@agentmorris
Copy link
Owner

agentmorris commented Jul 13, 2025

More specifically, I confirmed that adding an __init__.py to the "megadetector" folder doesn't break anything for me, so I don't mind adding it. So for your tests, I recommend removing the __init.py__ from the repo root, but leaving megadetector/__init__.py in place, and repeating your tests. If something still doesn't work, we should debug; that would suggest you are importing the package in an unexpected way (e.g. "from MegaDetector.megadetector").

@PetervanLunteren
Copy link
Contributor Author

I checked and importing like

from megadetector.detection.video_utils import VIDEO_EXTENSIONS

works!

No need for this PR. Thanks!

@PetervanLunteren
Copy link
Contributor Author

In fact, it was a really stupid mistake on my side (I was adding the wrong path to sys)... All good now!!

@agentmorris
Copy link
Owner

It was actually a good reminder to add the top-level init.py (in the package folder, not the repo root), so I did it anyway. It's not required for recent Python versions, but it's a best practice, and omitting it was lazy on my part. So, your mistake had a silver lining. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants