[Django] #36441: Add lazy loading support for GDAL library in `django.contrib.gis.gdal`

14 views
Skip to first unread message

Django

unread,
Jun 5, 2025, 10:57:05 AMJun 5
to django-...@googlegroups.com
#36441: Add lazy loading support for GDAL library in `django.contrib.gis.gdal`
-------------------------------------+-------------------------------------
Reporter: Josh Thomas | Type:
| Cleanup/optimization
Status: new | Component: GIS
Version: dev | Severity: Normal
Keywords: gdal | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
I'd like to propose enabling lazy loading of the GDAL library in
`django.contrib.gis.gdal`, matching the existing behavior of
`django.contrib.gis.geos` with the GEOS library.

Currently, calling `django.setup()` on projects using GeoDjango fails if
GDAL is not installed, even when GDAL functionality is not being used.
This creates an inconsistency in the GeoDjango framework where the GEOS
library supports lazy loading (implemented ~10 years ago) but GDAL does
not. The proposed change would implement lazy loading for the GDAL module
similar to the existing GEOS implementation, ensuring the GDAL library is
only loaded when actually accessed or used.

I have a working proof-of-concept in a
[https://github.com/joshuadavidthomas/django/tree/lazy-gdal branch] on my
fork of the Django repo ready for review
([https://github.com/django/django/compare/main...joshuadavidthomas:django
:lazy-gdal diff]).

The implementation follows the same approach as the original GEOS lazy
loading commit, wrapping the library loading in `SimpleLazyObject` and
converting function prototypes to use lazy loading. The public API remains
unchanged (everything imported in `django/contrib/gis/gdal/__init__.py`
continues to work as before), though the imports related to the GDAL
version from `django.contrib.gis.gdal.libgdal` now use lazy evaluation.
The existing test suite passes without modification, and I've added new
tests specifically for the lazy loading behavior. Documentation updates
have not been included yet as I wasn't sure if that was needed for a
change like this.

I have created both a [https://forum.djangoproject.com/t/proposal-lazy-
loading-for-django-contrib-gis-gdal/41198 forum post] and an
[https://github.com/django/new-features/issues/42 issue on django/new-
features]. Both have only been open for a few days, so not a ton of time
for people to see them. However, the initial reception has been positive,
with the forum post receiving several likes and the new-features issue
getting only thumbs-up reactions.
--
Ticket URL: <https://code.djangoproject.com/ticket/36441>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 5, 2025, 12:04:33 PMJun 5
to django-...@googlegroups.com
#36441: Add lazy loading support for GDAL library in `django.contrib.gis.gdal`
--------------------------------------+------------------------------------
Reporter: Josh Thomas | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: gdal | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Carlton Gibson):

* stage: Unreviewed => Accepted

Comment:

Thanks Josh, sounds good.

You said something about having a branch, so you can assign yourself and
open a PR.
--
Ticket URL: <https://code.djangoproject.com/ticket/36441#comment:1>

Django

unread,
Jun 5, 2025, 1:31:59 PMJun 5
to django-...@googlegroups.com
#36441: Add lazy loading support for GDAL library in `django.contrib.gis.gdal`
-------------------------------------+-------------------------------------
Reporter: Josh Thomas | Owner: Josh
Type: | Thomas
Cleanup/optimization | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: gdal | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Josh Thomas):

* owner: (none) => Josh Thomas
* status: new => assigned

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

Django

unread,
Jun 5, 2025, 2:39:57 PMJun 5
to django-...@googlegroups.com
#36441: Add lazy loading support for GDAL library in `django.contrib.gis.gdal`
-------------------------------------+-------------------------------------
Reporter: Josh Thomas | Owner: Josh
Type: | Thomas
Cleanup/optimization | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: gdal | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Josh Thomas):

* has_patch: 0 => 1

Comment:

[https://github.com/django/django/pull/19529 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/36441#comment:3>

Django

unread,
Jun 20, 2025, 9:09:24 AMJun 20
to django-...@googlegroups.com
#36441: Add lazy loading support for GDAL library in `django.contrib.gis.gdal`
-------------------------------------+-------------------------------------
Reporter: Josh Thomas | Owner: Josh
Type: | Thomas
Cleanup/optimization | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: gdal | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_docs: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36441#comment:4>

Django

unread,
Sep 16, 2025, 4:18:51 PMSep 16
to django-...@googlegroups.com
#36441: Add lazy loading support for GDAL library in `django.contrib.gis.gdal`
-------------------------------------+-------------------------------------
Reporter: Josh Thomas | Owner: Josh
Type: | Thomas
Cleanup/optimization | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: gdal | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Josh Thomas):

* needs_docs: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/36441#comment:5>

Django

unread,
Sep 18, 2025, 2:15:40 PMSep 18
to django-...@googlegroups.com
#36441: Add lazy loading support for GDAL library in `django.contrib.gis.gdal`
-------------------------------------+-------------------------------------
Reporter: Josh Thomas | Owner: Josh
Type: | Thomas
Cleanup/optimization | Status: assigned
Component: GIS | Version: dev
Severity: Normal | Resolution:
Keywords: gdal | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Josh Thomas):

I've updated the release note documentation updates from 6.0 to 6.1, as I
assume due to the feature freeze this PR will not be making it in to 6.0.
--
Ticket URL: <https://code.djangoproject.com/ticket/36441#comment:6>
Reply all
Reply to author
Forward
0 new messages