I\'ve been trying to setup my windows computer such that I can have a local postgreSQL with PostGIS extension. With this installed I hope to be able to create a project with
I have found the following to work for windows:
python
to check if your python is 32 or 64 bit.C:\OSGeo4W
or C:\OSGeo4W64
:
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.
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.
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'
Steps to follow:
C:\OSGeo4W
or C:\OSGeo4W64
:settings.py
:import os
GDAL_LIBRARY_PATH = r'C:\OSGeo4W\bin\gdal300'
set OSGEO4W_ROOT=C:\OSGeo4W
set PYTHON_ROOT=C:\Python3X
set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal
set PROJ_LIB=%OSGEO4W_ROOT%\share\proj
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:
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.