0

https://github.com/rtfd/readthedocs.org/tree/master/readthedocs/settings

I arrived at the link above from https://code.djangoproject.com/wiki/SplitSettings.

Can someone explain how that particular configuration works? Especially, the part played by settings/__init__.py containing just the name of a specific settings file (postgres.py in this case). I tried to do that to my project and django complained about not finding appropriate modules so how is it working for readthedocs?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Anuvrat Parashar
  • 2,960
  • 5
  • 28
  • 55

2 Answers2

2

It's actually pretty bad form what they did. __init__py in this case is a symlink to postres.py. When you open it on github it just shows the file it points at, because this is how symlinks look when coerced to display. It's not the same as just creating a file yourself and adding a line with a filename.

However, it's not good to actually commit symlinks, because they don't work on all operating systems. Additionally, the whole point here is to allow you to switch between postgres or sqlite3 settings, but if you remove __init__.py and create a new symlink to sqlite.py you've actually altered the source code at that point (because it was commited), and you wouldn't be able to pull new changes without reverting back to the original postgres.py symlink.

However, __init__.py has to be there or Python won't recognize it as a module. What they should have done is something like:

# __init__.py

from local import *

And, then create a symlink to whatever database settings file they wanted to use at local.py. That file, then, doesn't get committed, and there's no issues.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
0

In this case, settings/__init__.py is not a valid Python module, so it's very obviously a symlink to settings/postgres.py.

wrt/ __init__.py files, they are the marker used by Python to consider a folder a a Python package (cf http://docs.python.org/tutorial/modules.html#packages). This __init__.py file is imported when you import mypackage, just like it was a module.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118