82👍
I have found the following to work for windows:
- Run
python
to check if your python is 32 or 64 bit. - Install corresponding OSGeo4W (32 or 64 bit) into
C:\OSGeo4W
orC:\OSGeo4W64
:- Note: Select Express Web-GIS Install and click next.
- In the ‘Select Packages’ list, ensure that GDAL is selected; MapServer and Apache are also enabled by default, may be unchecked safely.
-
Make sure the following is included in your
settings.py
:import os if os.name == 'nt': import platform OSGEO4W = r"C:\OSGeo4W" if '64' in platform.architecture()[0]: OSGEO4W += "64" assert os.path.isdir(OSGEO4W), "Directory does not exist: " + OSGEO4W os.environ['OSGEO4W_ROOT'] = OSGEO4W os.environ['GDAL_DATA'] = OSGEO4W + r"\share\gdal" os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj" os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']
-
Run
python manage.py check
to verify geodjango is working correctly.
33👍
After updating some OSGEO4W on my Windows 10 Pro machine I started having problems with the GDAL bindings again. I previously used a combination of the solutions posted here and with this tutorial.
This is what works for me using Windows 10 Pro 64-bit, Django 3.0.6 and GDAL 3.0.4 using a python 3.7 virtual environment. I have tested it without OSGEO4W and it seems to work.
First, download the GDAL wheel from Christoph Gohlke’s Unofficial Windows Binaries for Python Extension Packages.
pip install "/path/to/GDAL‑3.0.4‑cp37‑cp37m‑win_amd64.whl"
Modify the libgdal.py file in the virtual envrironment site packages by adding ‘gdal300’ to line 23 of the Django GDAL package python file (/path/to/virtual_env/Lib/site-packages/django/contrib/gis/gdal/libgdal.py):
elif os.name == 'nt':
# Windows NT shared libraries
lib_names = ['gdal300', 'gdal204', 'gdal203', 'gdal202', 'gdal201', 'gdal20']
Finally, in your settings.py file in your Django project add
if os.name == 'nt':
VENV_BASE = os.environ['VIRTUAL_ENV']
os.environ['PATH'] = os.path.join(VENV_BASE, 'Lib\\site-packages\\osgeo') + ';' + os.environ['PATH']
os.environ['PROJ_LIB'] = os.path.join(VENV_BASE, 'Lib\\site-packages\\osgeo\\data\\proj') + ';' + os.environ['PATH']
- [Django]-Using Cloudfront with Django S3Boto
- [Django]-DRY way to add created/modified by and time
- [Django]-How to see details of Django errors with Gunicorn?
7👍
In my case (Windows10Pro+Python3.7.1), having the (automatically chosen) dll present was not enough, namely gdal111.dll
.
I realized that I also had gdal204.dll
located at C:\OSGeo4W\bin
and tried to “enrich” the list variable named lib_names
with 'gdal204'
, at line 24 (regarding Windows NT shared libraries) of %PYTHON_ROOT%\Lib\site-packages\django\contrib\gis\gdal\libgdal.py
, i.e.
#[...]
elif os.name == 'nt':
# Windows NT shared libraries
lib_names = ['gdal204', 'gdal202', 'gdal201', 'gdal20', 'gdal111', 'gdal110', 'gdal19']
#[...] ^^^^^^^
No negative consequences for now.
- [Django]-Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
- [Django]-How to Copy/Clone a Virtual Environment from Server to Local Machine
- [Django]-"_set" in a queryset object in Django
3👍
For Microsoft Windows 10 & Python3.6.8, I installed GDAL 2.3.3 from Unofficial Windows Binaries for Python Extension Packages, modified libgdal.py adding gdal203
in the lib_names list env\Lib\site-packages\django\contrib\gis\gdal\libgdal.py
.
Finally, added osgeo and proj to the PATH, and set the GDAL_LIBRARY_PATH as below (beginning of settings.py):
os.environ['PATH'] = os.path.join(BASE_DIR, r'env\Lib\site-packages\osgeo') + ';' + os.environ['PATH']
os.environ['PROJ_LIB'] = os.path.join(BASE_DIR, r'env3\Lib\site-packages\osgeo\data\proj') + ';' + os.environ['PATH']
GDAL_LIBRARY_PATH = os.path.join(BASE_DIR, r'env\Lib\site-packages\osgeo\gdal203.dll')
In this case, env is my Python environment.
- [Django]-Django submit two different forms with one submit button
- [Django]-ValueError: Related model u'app.model' cannot be resolved
- [Django]-Django dump data for a single model?
1👍
Steps to follow:
- Run python to check if your python is 32 or 64 bit.
- Install corresponding OSGeo4W (32 or 64 bit) into
C:\OSGeo4W
orC:\OSGeo4W64
:
Note: Select Express Web-GIS Install and click next. - In the ‘Select Packages’ list, ensure that GDAL is selected; MapServer and Apache are also enabled by default.
- Make sure the following is included in your
settings.py
:
import os
GDAL_LIBRARY_PATH = r'C:\OSGeo4W\bin\gdal300'
- Now, run the server still if it doesn’t work. Run the following commands in terminal.
set OSGEO4W_ROOT=C:\OSGeo4W
set PYTHON_ROOT=C:\Python3X
set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal
set PROJ_LIB=%OSGEO4W_ROOT%\share\proj
- [Django]-Password field in Django model
- [Django]-Django: show the count of related objects in admin list_display
- [Django]-Django error: needs to have a value for field "…" before this many-to-many relationship can be used
1👍
I had the same error “The specified module could not be found,” even though gdal204.dll
was present at the expected location, with the right architecture (which I verified by adding asserts in the Python code and loading the DLL from a C program).
It turned out to be an issue with the Python 3.7 app from the Microsoft Store, the one that automatically installs the first time you type python
on the command line.
This version of Python refuses to load the GDAL DLL; I don’t know if it’s a bug or a security feature.
I fixed the issue by:
- uninstalling the Python app
- removing the app execution aliases (as explained in the app description)
- installing the regular Python package from python.org
- [Django]-Disable Django South when running unit tests?
- [Django]-What are the limitations of Django's ORM?
- [Django]-Django ORM – objects.filter() vs. objects.all().filter() – which one is preferred?
1👍
- Download GDAL wheel file that is supported for your platform from here.
- Open the command window where the downloaded file is located and activate your virtual environment.
- Then install the wheel using command
pip install name_of_the_file
.
You will see osgeo folder has been created in the location ‘…\Envs\my_django\Lib\site-packages\’ .
-
Go to osgeo folder and copy the entire path of your gdalxxx.dll file and add to setting.py file as gdal library path. For example
GDAL_LIBRARY_PATH = r'C:\Users\WIN8\Envs\my_django\Lib\site-packages\osgeo\gdal300.dll'
- [Django]-How to show query parameter options in Django REST Framework – Swagger
- [Django]-What are the steps to make a ModelForm work with a ManyToMany relationship with an intermediary model in Django?
- [Django]-How to escape {{ or }} in django template?
1👍
Creating the map compatible database
I recently got a new laptop and had to install Python and the related software on my new machine. I was trying to create a geodjango compatible database using OSGeo4W and had forgotten about the role of PostgreSQL and pgAdmin4.
When you install the latest PostgreSQL setup from https://www.enterprisedb.com/downloads/postgres-postgresql-downloads, you get pgAdmin4 at the same time.
Note – Ecommerce is my personal server group.
When you create a PostgreSQL 13 server, your server group will be different to mine.
When I first opened pgAdmin4 on my new machine, I kept the default username ‘postgres’. If when you first logged into pgAdmin4 you chose your own username, you will have to change the values below accordingly.
Connecting to the database
In my code I had
if os.name == 'nt':
import platform
OSGEO4W = r"C:\OSGeo4W"
if '64' in platform.architecture()[0]:
OSGEO4W += "64"
assert os.path.isdir(OSGEO4W), "Directory does not exist: " + OSGEO4W
os.environ['OSGEO4W_ROOT'] = OSGEO4W
os.environ['GDAL_DATA'] = OSGEO4W + r"\share\gdal"
os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']
GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal300.dll'
I was getting an error message regarding the GDAL_LIBRARY_PATH so checked Windows Explorer. On discovering I did not have a gdal300.dll file, I changed my code to
GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal301.dll'
- [Django]-Django AdminForm field default value
- [Django]-RuntimeWarning: DateTimeField received a naive datetime
- [Django]-Django Call Class based view from another class based view
0👍
Just to follow up on the nice and detailed answer of Udi (cannot comment directly as my rep is under 50, it is the answer marked as the most useful);
After many hours I tried his offered solution which also did not work for me.
I was getting the following error:
OSError: [WinError 193] %1 is not a valid Win32 application
But I stayed there and found out that although I’m running 64 python and operating system (for sure), it kept looking for 32 bit (OSGeo4W
) folder. What eventually let me pass was to copy contents of the OSGeo4W64
folder to the OSGeo4W
. Hope it will save you time.
One more note:
Be sure that you edit libgdal.py
file in your environment folder. It may exist in more than one place – your python folder and environment folder – if you edit the libgdal
in your python dir, it is not going to work.
- [Django]-Suppress "?next=blah" behavior in django's login_required decorator
- [Django]-How to debug Jinja2 template?
- [Django]-How to get all users of a group in Django?
0👍
pip install GDAL
(From https://www.lfd.uci.edu/~gohlke/pythonlibs/)
Add In %PYTHON_ROOT%\Lib\site-packages\django\contrib\gis\gdal\libgdal.py
import osgeo
Check Gdal Vesion In: %PYTHON_ROOT%\Lib\site-packages\osgeo\gdal303.dll
lib_names = ['gdal303', 'gdal202', 'gdal201', 'gdal20', 'gdal111', 'gdal110', 'gdal19']
- [Django]-Django accessing ManyToMany fields from post_save signal
- [Django]-Django – makemigrations – No changes detected
- [Django]-Django model object with foreign key creation
0👍
Here is a solution to this gdal problem in Django: https://www.dark-hamster.com/application/how-to-solve-error-message-could-not-find-the-gdal-library-when-running-django-application-in-microsoft-windows/?utm_source=pocket_saves.
-
First of all ensure that you have downloaded the OSGeo4W package from here. Install it to your computer. It will take quite some time since the full package is >1GB.
-
Ensure that you have a folder
C:\OSGeo4W
in your C Drive. -
Go to
C:\OSGeo4W\bin
and check for a file such asgdal306.dll
. In my case I havegdal306.dll
andgdal307.dll
. I went with the former. -
Go to your
settings.py
file in your Django project. -
If you pasted your Django error somewhere, it read something like this:
django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal202", "gdal201", "gdal20", "gdal111", "gdal110", "gdal19"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.
We will give it a taste of its own medicine by creating a variable GDAL_LIBRARY_PATH
in your settings.py
file.
- Copy the path to your
gdal306.dll
file from OSGeo4W folder. In my case it was,C:\OSGeo4W\bin\gdal306.dll
. Paste it as the path to theGDAL_LIBRARY_PATH
like so:
GDAL_LIBRARY_PATH = r'C:\OSGeo4W\bin\gdal306.dll'
Note – make sure to add r’absolute_path’ instead of ‘absolute_path’ otherwise this error will come – FileNotFoundError: Could not find module ‘C:\OSGeo4in\gdal307.dll’ (or one of its dependencies). Try using the full path with constructor syntax.
- Run your django project once more, if all is ok, there shouldn’t be that error again.
- [Django]-Django – Highlight Navigation based on current page?
- [Django]-Sort order of Django Admin records
- [Django]-Django Rest Framework — no module named rest_framework