0

in my Django project, I have organised the settings as a module with a file structure like this:

/settings/
  __init__.py
  base.py
  prod.py
  dev.py
  dev_user_a.py
  dev_user_b.py
  prod_user_a.py
  prod_user_b.py    etc.

They are working as a hierarchy, i.e. def_user_a.py imports dev.py, dev.py imports base.py. __init__.py imports one of the 'leaves', i.e. dev_user_a.py, this is how each user can choose his settings. This works fine, except that I cannot seem to exclude /settings/__init__.py from git, which means local changes to this file are likely to be accidentally broadcasted to other users.

Two questions:

  1. Is there a better way to achieve this, and how?
  2. Why does adding __init__.py to .gitignore (or .git/info/exclude) not work?
torek
  • 448,244
  • 59
  • 642
  • 775
  • Has it already been committed? – evolutionxbox Apr 28 '21 at 09:00
  • Yes, by another user, then I pulled it, unawares that this was happening, and for a while worked on his settings. Now I've changed it back to my values, and I haven't staged it, but it keeps hanging around in my uncomitted changes. When I try git check-ignore trees/trees/settings/*, it correctly lists settings/dev_xxx.py, but not __init__.py – Angelika Sajani Apr 28 '21 at 09:03
  • .gitignore contains (amongst other stuff) those two lines trees/trees/settings/__init__.py trees/trees/settings/dev_*.py – Angelika Sajani Apr 28 '21 at 09:05
  • If it's being tracked then it won't be ignored. – evolutionxbox Apr 28 '21 at 09:06
  • Ah, I had to use git rm --cached trees/trees/__init__.py, thanks. – Angelika Sajani Apr 28 '21 at 09:10
  • Sure, but remember that this will remove it from git for the other use too (if they merge or use your branch) – evolutionxbox Apr 28 '21 at 09:10
  • "*…this will remove it from git for the other use too (if they merge or use your branch)*" This will remove the file from the working tree even for @AngelikaSajani when she switches to a different branch and then back. – phd Apr 28 '21 at 10:09

1 Answers1

0

As already noted in comments, a tracked file is not going to be ignored, even if its name is listed in an exclusion file. See also this related question. Note that a tracked file is any file that is currently in Git's index and a committed file will be extracted into Git's index during git checkout, so any committed file becomes tracked by default.

It's important not to omit or remove __init__.py since its presence turns a module into a "package": see §6.4 of the official Python 3 documentation for instance. Removing it will remove the package-ness of this directory.

In this particular case, the __init__.py file should probably just be empty. Users can import the correct settings directly. Or, because this is a Django setup, they should probably use the DJANGO_SETTINGS_MODULE environment variable instead, as described in the Django documentation. (Note: Django users may have better answers here. I have never used Django myself.)

torek
  • 448,244
  • 59
  • 642
  • 775