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.

Eli O.
  • 1,543
  • 3
  • 18
  • 27

1 Answers1

5

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.

cezar
  • 11,616
  • 6
  • 48
  • 84
rahul.m
  • 5,572
  • 3
  • 23
  • 50
  • 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. Mar 28 '19 at 07: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 Mar 28 '19 at 07:43