3

I have many __init__.py files in my Django project and apps folder,

I think most were generated by Django when I initialised my project and its apps

I have seen in How do I delete DB (sqlite3) in Django 1.9 to start from scratch? that apparently we shouldn't delete the __init__.py file in the migration folders.

Can I delete the others, empty __init__.py files ? It s just that they kind of clutter the directory tree, so if they are useless I would rather remove them.

Edit : I just tried and deleted them and I didn t see any changes to the functionnalities of my website, everything worked fine. But as others pointed out they are definitely not useless so I ll leave them be.

1
  • 2
    Short answer, don't. Long answer 👆
    – Vaibhav Vishal
    Commented Mar 28, 2019 at 7:30

1 Answer 1

6

Basically the __init__.py file makes Python treat directories containing it as modules.

So by deleting that file Django doesn't treat it as a module and it will stop working.

So it is bad idea to delete those files.

2
  • I tried, deleted them all and saw no change whatsoever in the functionnalities of my website. But as you pointed out they are not useless so I ll leave them where they are.
    – Eli O.
    Commented Mar 28, 2019 at 7:38
  • 4
    Well, it depends on version of Python You're using. Newer Python versions do NOT require __init__.py file in order to recognize modules.
    – Fejs
    Commented Mar 28, 2019 at 7:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.