Resource loading (Django without a filesystem)

Skip to first unread message

Peter Baumgartner

Jun 27, 2019, 3:09:18 PM6/27/19
to Django developers (Contributions to Django itself)
I'm interested in using PyOxidizer [1] to create single-file executable Django projects. This currently isn't possible because of all the places Django assumes it is operating on a proper filesystem. I haven't done a full audit, but templates, migrations, and static files come to mind. Python has full support this scenario (aka resource loading), but it would require some changes to Django internals to use it. One of the biggest hurdles I see on the surface is that you can only load a resource from a Python module (not from a sub-directory). That being said, I have a feeling resource loading could be added such that users could opt-in or backwards compatibility is maintained with the current implementation.

Some additional reading/watching on the subject:

* Barry Warsaw talk on resource loading:

I'm posting here to see if there is general support for this and to discuss what sort of changes would be required. Thanks!

Markus Holtermann

Jun 27, 2019, 3:39:53 PM6/27/19
to Django developers
Hi Peter,

PyOxidizer looks indeed super interesting. Talking about templates and specifically Jinja2 templates, they are internally converted to the Python AST if I'm not mistaking. Turning them into Python modules that a new Jinja2ModuleTemplateLoader could load doesn't seem like that far fetched.

Similarly for Django's migration files. Swapping out the MigrationLoader would already be sufficient.

I'd definitely be interested to see what's needed to change in core to have a 3rd party package provide the necessary support.


> --
> You received this message because you are subscribed to the Google
> Groups "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
> To post to this group, send email to
> Visit this group at
> To view this discussion on the web visit
> <>.
> For more options, visit

Andrew Godwin

Jun 27, 2019, 4:40:18 PM6/27/19
to Django developers (Contributions to Django itself)
My impression reading over the problem a little yesterday was that we could work to provide a common "get me a resource" abstraction in Django that papers over the couple of different ways it has to work, though I haven't looked super far into things that require directory listing (e.g. migrations) rather than direct file access (like templates).

It would be nice to investigate this a bit more, though. If we can get Django compatible, or work with PyOxidiser if we find a reasonable workaround they could implement, it would be great.


Peter Baumgartner

Jun 27, 2019, 5:01:08 PM6/27/19
The big issue I see is that a resource must reside directly in a
Python module. You can not load a resource from a child directory,
e.g. I can load "index.html" from the Python module
"myproject.templates", but I can't load "app1/index.html" from the
same module.

This would require developers to scatter files in every
template directory. I think migrations would be easy (you can list
Python files in a module without the filesystem). I need to look at
how staticfiles works a little closer. I think we could still use
__file__ for collectstatic (you would run that before converting your
project), but I'm not sure what Django needs access to at runtime

Pytz is also an issue. It looks like an easier fix there because the
project controls the "resource" directories and could sprinkle in the
necessary files. I filed an issue to start the discussion
there as well,
> To view this discussion on the web visit

Andrew Godwin

Jun 27, 2019, 5:05:26 PM6/27/19
to Django developers (Contributions to Django itself)
Well, remember that in Python 3 you don't need an file to have something be a module (because of, but I wonder if there still needs to be a proper discovery mechanism that flags that they should be considered as implicit packages/modules.


Peter Baumgartner

Jun 27, 2019, 5:17:01 PM6/27/19
Unfortunately you can't load resources from implicit namespace
packages. It raises `FileNotFoundError: Package has no location
<module 'myapp' (namespace)>`
The appears to be required.
> To view this discussion on the web visit
Reply all
Reply to author
0 new messages