Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSError exception #125

Open
andywar65 opened this issue Jun 2, 2024 · 2 comments
Open

OSError exception #125

andywar65 opened this issue Jun 2, 2024 · 2 comments

Comments

@andywar65
Copy link

django-geojson tries to use django.contrib.gis objects, but if ImportError or ImproperlyConfigured exceptions are raised, it fallbacks to custom nogeos objects. In my case (working on Windows) an OSError exception is raised, breaking the code. I added this exception to a fork of the library and everything seems to work fine:

# djgeojson/templatetags/geojson_tags.py
except (ImportError, ImproperlyConfigured, OSError):

# djgeojson/fields.py
except (ImportError, OSError):

# djgeojson/serializers.py
except (ImportError, ImproperlyConfigured, OSError):

Of course my issue depends on a local configuration, but the fix suggested makes life easier to the unexperienced.
Thanks a lot for the library, by the way!

@Gagaro
Copy link
Member

Gagaro commented Jun 10, 2024

I'm not sure I understand, why did you have the OSError in the first place?

@andywar65
Copy link
Author

I'm on Windows 10 Pro, build 19045.4474 (not WSL)
Let's say I create a virtual environment and activate it, and pip install django and django-geojson.
Then I start a django project and add 'djgeocad' to INSTALLED_APPS.
I migrate and run the server, that crashes. This is the error I get:

Exception in thread django-main-thread:
Traceback (most recent call last):
[...]
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\djgeojson\templatetags\geojson_tags.py", line 8, in <module>
    from django.contrib.gis.db.models.fields import GeometryField
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\db\models\__init__.py", line 3, in <module>
    import django.contrib.gis.db.models.functions  # NOQA
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\db\models\functions.py", line 3, in <module>
    from django.contrib.gis.db.models.fields import BaseSpatialField, GeometryField
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\db\models\fields.py", line 3, in <module>
    from django.contrib.gis import forms, gdal
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\forms\__init__.py", line 3, in <module>
    from .fields import (  # NOQA
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\forms\fields.py", line 2, in <module>
    from django.contrib.gis.gdal import GDALException
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\gdal\__init__.py", line 29, in <module>
    from django.contrib.gis.gdal.datasource import DataSource
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\gdal\datasource.py", line 41, in <module>
    from django.contrib.gis.gdal.driver import Driver
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\gdal\driver.py", line 5, in <module>
    from django.contrib.gis.gdal.prototypes import ds as vcapi
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\gdal\prototypes\ds.py", line 10, in <module>
    from django.contrib.gis.gdal.libgdal import lgdal
  File "D:\venvs\test-djgeojson\env\Lib\site-packages\django\contrib\gis\gdal\libgdal.py", line 69, in <module>
    lgdal = CDLL(lib_path)
            ^^^^^^^^^^^^^^
  File "C:\Users\Andrea\AppData\Local\Programs\Python\Python312\Lib\ctypes\__init__.py", line 379, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [WinError 127] The specified procedure could not be found

It is probably a configuration error.
If you go to djgeojson/templatetags/geojson_tags.py from line 7 to 12 you find this:

try:
    from django.contrib.gis.db.models.fields import GeometryField
    from django.contrib.gis.geos import GEOSGeometry
except (ImportError, ImproperlyConfigured):
    from ..nogeos import GEOSGeometry
    from ..fields import GeometryField

If you add the OSError to the exceptions (same for the other two files in the opening message of the issue), everything works fine.
Thank you for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants