#30950: Replace __file__ with importlib.resources
-------------------------------------------+------------------------
Reporter: John Vandenberg | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.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 |
-------------------------------------------+------------------------
`__file__` is an optional attribute of modules, and it either is
added/modified by magic logic like Nuitka, PyInstaller, cx_Freeze, and
each new freezer needs to add per-package hacks to get Django working, and
need to be fixed whenever Django internals change, or isnt provided by
freezers like PyOxidizer which refuse to play that cat & mouse game (c.f.
https://github.com/indygreg/PyOxidizer/issues/69).
The solution is to use `importlib.resources` where-ever the code is
accessing data files inside the Django package, or other modules.
e.g. under PyOxidizer:
```
File "django.test", line 3, in <module>
File "django.test.client", line 14, in <module>
File "django.core.handlers.base", line 8, in <module>
File "django.urls", line 1, in <module>
File "django.urls.base", line 8, in <module>
File "django.urls.exceptions", line 1, in <module>
File "django.http", line 5, in <module>
File "django.http.response", line 15, in <module>
File "django.core.serializers", line 23, in <module>
File "django.core.serializers.base", line 7, in <module>
File "django.db.models", line 3, in <module>
File "django.db.models.aggregates", line 5, in <module>
File "django.db.models.expressions", line 8, in <module>
File "django.db.models.fields", line 11, in <module>
File "django.forms", line 6, in <module>
File "django.forms.boundfield", line 4, in <module>
File "django.forms.widgets", line 26, in <module>
File "django.forms.renderers", line 16, in <module>
NameError: name '__file__' is not defined
```
To make this feature contained, I would limit it to runtime code of
Django, and exclude instances of `__file__` in docs and templates deployed
into new projects which need to be "manage.py-relative-paths", such as
those in https://github.com/django/django/pull/12005
--
Ticket URL: <https://code.djangoproject.com/ticket/30950>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/049.57e4f56880a846dcb30d39ddc80514ab%40djangoproject.com.