[Django] #33162: AttributeError: 'Settings' object has no attribute '__file__'

10 views
Skip to first unread message

Django

unread,
Oct 1, 2021, 7:09:01 AM10/1/21
to django-...@googlegroups.com
#33162: AttributeError: 'Settings' object has no attribute '__file__'
------------------------------------------+------------------------
Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: Core (Other) | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+------------------------
Sometimes you have no clue which settings file is active.

For other files I use this pattern, to see which actual file is loaded:

```
assert 0, mylib.__file__
```

Unfortunately this does not work for `django.conf.settings`:

```
AttributeError: 'Settings' object has no attribute '__file__'
```

It would be super cool, if this attribute would be available.

--
Ticket URL: <https://code.djangoproject.com/ticket/33162>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 1, 2021, 7:09:43 AM10/1/21
to django-...@googlegroups.com
#33162: AttributeError: 'Settings' object has no attribute '__file__'
--------------------------------+--------------------------------------

Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: Core (Other) | Version: 3.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Description changed by Thomas Güttler:

Old description:

> Sometimes you have no clue which settings file is active.
>
> For other files I use this pattern, to see which actual file is loaded:
>
> ```
> assert 0, mylib.__file__
> ```
>
> Unfortunately this does not work for `django.conf.settings`:
>
> ```
> AttributeError: 'Settings' object has no attribute '__file__'
> ```
>
> It would be super cool, if this attribute would be available.

New description:

Sometimes you have no clue which settings file is active.

For other files I use this pattern, to see which actual file is loaded:

{{{
assert 0, mylib.__file__
}}}

Unfortunately this does not work for `django.conf.settings`:

{{{
AttributeError: 'Settings' object has no attribute '__file__'
}}}

It would be super cool, if this attribute would be available.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/33162#comment:1>

Django

unread,
Oct 1, 2021, 7:13:13 AM10/1/21
to django-...@googlegroups.com
#33162: AttributeError: 'Settings' object has no attribute '__file__'
--------------------------------+--------------------------------------

Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: new
Component: Core (Other) | Version: 3.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Description changed by Thomas Güttler:

Old description:

> Sometimes you have no clue which settings file is active.


>
> For other files I use this pattern, to see which actual file is loaded:
>

>
> {{{
> assert 0, mylib.__file__
> }}}
>
> Unfortunately this does not work for `django.conf.settings`:
>
> {{{
> AttributeError: 'Settings' object has no attribute '__file__'
> }}}
>
> It would be super cool, if this attribute would be available.

New description:

Sometimes you have no clue which settings file is active.

For other files I use this pattern, to see which actual file is loaded:

{{{
assert 0, mylib.__file__
}}}

Unfortunately this does not work for `django.conf.settings`:

{{{
AttributeError: 'Settings' object has no attribute '__file__'
}}}

It would be super cool, if this attribute would be available.

Of course I could use:

{{{
assert 0, os.environ['DJANGO_SETTINGS_MODULE']
}}}

But sometimes the env var is not the actual file.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/33162#comment:2>

Django

unread,
Oct 1, 2021, 7:43:57 AM10/1/21
to django-...@googlegroups.com
#33162: Add __file__ to Settings.
--------------------------------+--------------------------------------

Reporter: Thomas Güttler | Owner: nobody
Type: New feature | Status: closed

Component: Core (Other) | Version: 3.2
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => wontfix


Comment:

You can use `settings.SETTINGS_MODULE` to check the actual module and
`importlib` to get the file, e.g.
{{{
>>> from django.conf import settings
>>> import importlib
>>> importlib.import_module(settings.SETTINGS_MODULE).__file__
'/django/tests/test_pg.py'
}}}
I don't think that a hook is needed in Django itself.

--
Ticket URL: <https://code.djangoproject.com/ticket/33162#comment:3>

Reply all
Reply to author
Forward
0 new messages