Create the "nearbyshops" App
In this video you will be creating a Django app. You’ve already created the Django project which is the main entry point to your website.
This is the current structure of your web app so far. What you are looking at is considered the Django project portion of your web application.
You can see the utilities are there for configuration and routing, but there are no web pages yet, and no interactivity.
This is where the Django app comes in. A request will arrive at the front door of our Django project. Urls.py will determine the destination view for the particular request and direct it to the appropriate Django app.
00:00 Welcome to video number six in this series on making a location-based web app in Django and GeoDjango. In this video, you will be creating a Django app. You’ve already created the Django project, which is the main entry point to your website.
00:16 This is the current structure of your web app so far. What you are looking at is considered the Django project portion of your web application. You can see the utilities are there for configuration and routing, but there are no web pages yet and no interactivity.
00:31
This is where the Django app comes into play. A request will arrive at the front door of our Django project. urls.py
will determine the destination view for the particular request and direct it to the appropriate Django app.
00:47
The app, in turn, may have its own urls.py
to do additional routing, or it processes the corresponding view specified in the main urls.py
.
00:57 A response is then formed and sent back through the main site.
01:03
With the shops
project in place, you will create the nearbyshops
app. Your shops
project can have multiple apps, but for this tutorial you will only be creating one.
01:15
To create a Django app in a project, use the manage.py
utility created by the project script when the project was initiated. At your terminal prompt, type python manage.py startapp nearbyshops
.
01:31
Let’s see the effect of the startapp
script. Notice a new folder has been created with the name of our app. Inside, there are some additional files. The app was successfully created.
01:43
But there’s one more step. The shops
project needs to know about the new app, therefore we need to add it to the INSTALLED_APPS
list in settings.py
.
01:53
The nearbyshops
app is now a component of the shops
project—great job! You’ve done a lot of work and you may be curious to see if your website starts.
02:02
Let’s see! To launch your site, use the manage.py
utility again. At your terminal prompt type python manage.py runserver
.
02:13
Your website should launch on localhost
port 8000
. Click the loopback URL provided by the runserver
output. You should see the default page for a new Django project.
02:25 To stop your site just press Control + C back in your terminal. The next step is to get our model set up and some data into our database. Let’s do that in the next video.
Kevin M on May 13, 2019
Done, I finally could debug it!!! I had to add these: GEOS_LIBRARY_PATH = r’C:\OSGeo4W\bin\geos_c.dll’ GDAL_LIBRARY_PATH = r’C:\OSGeo4W\bin\gdal204.dll’
ciaranraymer on May 13, 2019
I’m using Spyder and have not been able to use any of these commands in the terminal, instead I’ve had to use the command prompt. I’m pretty stuck now on the “python manage.py startapp nearby shops” part, as the cmd prompt just opens the script and doesn’t create a folder - any tips for a Windows user?
Jackie Wilson RP Team on May 13, 2019
Are you running your command prompt as admin? I’m going to find a Windows machine and follow my own instructions. Sounds like I have to include some extra steps for Win users.
Darryl Rich on May 19, 2019
I’m running this on Windows 10 and experiencing the same issues as Kevin.
error (django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried “gdal203”, “gdal202”, “gdal201”, “gdal20”, “gdal111”). Is GDAL installed? If it is, try setting GDAL_LIBR ARY_PATH in your settings. )
Not sure where to make the path changes; in the os environment, the venv, or the django manage itself.
Jackie Wilson RP Team on May 20, 2019
I’m putting together some separate instructions for Windows users. Sorry you’re running into trouble. You could set the path in any of those places, or in the settings.py file. Also, you’ll get this error and similar if you have 32-bit python with a 64-bit gdal installation (or vice versa). I mainly work on a Mac, but can appreciate the majority GIS folks probably work in Windows. I’m currently trying to get a clean setup going on Win 10. I’ll update this tutorial when I have that finished.
Justin Cletus on June 30, 2019
Hi Jackie, I am getting this issue after database connection added in seetings.py file, conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: received invalid response to SSL negotiation: - Is there anything I missed in setup?
Jackie Wilson RP Team on July 2, 2019
Hi Justin, first thing to check would be if you have the right port configured. Postgres will default to 5432 but the would be different if you manually changed it, or if you have multiple instances running.
Harshida Rami on July 7, 2019
Hi Jackie,I am using windows and experiencing error same as above
error (django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried “gdal203”, “gdal202”, “gdal201”, “gdal20”, “gdal111”). Is GDAL installed? If it is, try setting GDAL_LIBR ARY_PATH in your settings. ). not sure where and how add path.
ChrisF on Aug. 24, 2019
I am getting the errors as all the others are getting with this. I have checked my folder for any of these: “gdal203”, “gdal202”, “gdal201”, “gdal20”, “gdal111” But it seems like they are not installed. I have tried as much as I can but can’t get it to work.
Does me using Python 3.7 have anything to do with it? I know you said it is not compatible with QGIS but would that have an effect on this part?
Thanks
hithard on Sept. 22, 2019
same error as most of others getting here
maju1005 on Oct. 5, 2019
Hi Jackie, Do you have any updates on issues with GDAL on Windows machines? I just run into the same problem as rest of Win users.
Do you know whether this issue occurs also on Linux?
Thanks in advance for your help and have a nice day!
nosizwe31 on Oct. 7, 2019
Hi maju1005
found solution from this link : stackoverflow.com/questions/49139044/geodjango-on-windows-could-not-find-the-gdal-library-oserror-winerror-12
arjun0910 on Oct. 10, 2019
I’m using a Mac and facing the issue when I type python manage.py startapp nearbyshops
The error I receive is:- Could not find the GDAL library (tried “gdal”, “GDAL”, “gdal2.3.0”, “gdal2.2.0”, “gdal2.1.0”, “gdal2.0.0”, “gdal1.11.0”). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.
Please help me, What should i do
kylebronstein on Nov. 4, 2019
I’m on mac and I was getting that error, now I’m getting an error saying it referenced a directory and can’t find an image. I don’t know what to do.
rupaklama on Nov. 6, 2019
I ran “brew install gdal” on my mac, fixed this issue! The installion takes a while.
natej815 on Nov. 15, 2019
Getting this error. would love to get this fixed asap please!
django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (t ried “gdal203”, “gdal202”, “gdal201”, “gdal20”, “gdal111”). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.
kal on Nov. 29, 2019
For windows 10 users getting the GDAL not found error I found this specific solution using the link above to stack overflow. I added the block of path logic to the top of the settings.py project file just below the existing import os statement.
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’]
Masa on Dec. 3, 2019
I ran into the same issue of django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (t ried “gdal203”, “gdal202”, “gdal201”, “gdal20”, “gdal111”). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.
I’m using MacOS 10.1.6
High Sierra. Homebrew installed Python3.7.4. I used pyenv and pipenv to set python 3.6.8 in my virtualenv.
How the django find a gdal installed by GGIS?
After all, I used brew install glad
. After it was done, python manage.py startapp
was run without error.
jlrv16 on Dec. 19, 2019
I got the same issue (on win10 64 bits) python 3.8 . I have installed OSgeo4W64 on my system. To solve this issue just include the following in the settings.py of your project.
GEOTH_LIBRARY_PATH = 'C:\\OSGeo4W64\\bin'
GDAL_LIBRARY_PATH = 'C:\\OSGeo4W64\\bin\\gdal300.dll'
hope it will help hose who like me became mad with it!
Pacifique on Dec. 24, 2019
Hi Jackie, I got the same issue (on win10 64 bits) , I have followed this stack overflow solution stackoverflow.com/questions/49139044/geodjango-on-windows-could-not-find-the-gdal-library-oserror-winerror-12
I have installed OSgeo4W64 on my system. and added this block of code in the 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’]
But I now get this error: OSError: [WinError 127] The specified module could not be found
How can I solve this issue ?
travisbrown022 on Jan. 3, 2020
thank you @nosizwe31 your solution worked for me too on Windows.
Rony Efendy on Feb. 29, 2020
Hi, I am stuck here.
I’m using Windows 10 64-bit.
Ricky White RP Team on March 1, 2020
Hi Rony. You’re going to have to be a bit more specific. What’s got you stuck?
bhoo on March 15, 2020
Hi All, Pls. help me resolve this issue , I am using window 8.1 32 bit system and tring to run python manage.py startapp nearbyshops command to create app but getting following error: django.core.exceptions.Improperly Configured: Could not find the GDAL library (tried “gdal204”, “gdal203”, “gdal202”, “gdal201”, “gdal20”). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.
Ricky White RP Team on March 16, 2020
Hi indibhopal. Others have had the same issue. Please see the comments above.
dthomas01 on March 16, 2020
Hi everyone, I’m late to the party. Yes, I hit the same wall but all your suggestions helped me get a workaround for me. I’m running Windows 10 Pro (64-bit) on a laptop, Python 3.7 (64-bit), PostgreSQL 64-bit, QGIS 64-bit. At the top of the settings.py file, right beneath the “import os” statement (like earlier posts indicated), I pasted all this stuff. My QGIS had installed OSGeo4 here “C:\Program Files\QGIS 3.12" but it wasn’t the embedded spaces. Even the official Django 3.0 install notes said it needs some OS environment path variables added. Simply pasting the following did the trick, to allow the: python manage.py startapp nearbyshops to run to completion. Time has passed. So Python 3.7 is now supported. Here it is:
GEOS_LIBRARY_PATH = ‘C:\Program Files\QGIS 3.12\bin\geos_c.dll’ GDAL_LIBRARY_PATH = ‘C:\Program Files\QGIS 3.12\bin\gdal300.dll’
if os.name == ‘nt’: # raw str path where QGIS Win 10 installer placed GDAL base. OSGEO4W = r”C:\Program Files\QGIS 3.12” 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’]
bhoo on March 17, 2020
Hi,Ricky White and dthomas01, I tried as u suggested, my earlier error is gone but getting a new error as :OSError: [WinError 126] The specified module could not be found: would appreciate if anyone could help me to resolve this new problem. thanks in advance.
dthomas01 on March 17, 2020
- Since you are using an old Windows 32-bit, I hope you installed all 32-bit versions of the other stuff (PostgreSQL, QGIS, etc). Otherwise, you might get a mismatch.
- I’m running QGIS version 3.12, the latest. So that’s why my paths show that number. Do you in fact have a gdal.dll file in your C:\Program Files\QGIS 3.12\bin\ folder? If not then just need to fix your paths. Otherwise, there is one last thing to try.
-
What I did to see which exact file the libgdal.py was choking on was to look at the very last portion of my Traceback error and notice that the error occurred in this file (C:\Users(myuserid)\Anaconda3\lib\ctypes_init.py) at a line of code within class CDLL() having function “def init” which contained this code block:
if handle is None: self._handle = _dlopen(self._name, mode) else: self._handle = handle
The error occurred right at the dlopen() call. So I just added my own little print() command to tell me what the input value was for self.name like so:
if handle is None:
print("DEBUG> opening CDLL file=", self._name)
self._handle = _dlopen(self._name, mode)
else:
self._handle = handle
Then retried the earlier failed: python manage.py startapp nearbyshops
When it errored again, I look way up top in the console for the first 10 lines of system responses and saw my “DEBUG> opening…” printed the name: gdal300.dll Yours might be different…can’t say. At least, pinpointing which file it cannot find is a step in the right direction.
bhoo on March 18, 2020
Hi dthomas01, Appreciate your efforts,my print output=DEBUG> opening CDLL file=kernel32 , is it something to do with windows 8 32 os? kindly advise further.
Mydhe on April 7, 2020
Thank you jlrv16, You fix helped me with this error: django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (t ried “gdal203”, “gdal202”, “gdal201”, “gdal20”, “gdal111”). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.
Addidng the path in my settings.py as below. GEOTH_LIBRARY_PATH = ‘C:\OSGeo4W64\bin’ GDAL_LIBRARY_PATH = ‘C:\OSGeo4W64\bin\gdal300.dll’
But before that, after activating my virtual environment i installed GDAL into it as GDAL might be installed Globally and not inside your virtual environment using this, 1. python -m pip install C:/Users/Dlu2/Downloads/GDAL-3.0.4-cp36-cp36m-win_amd64.whl
Am not sure if this had anything to do with clearing the error but just a step i thought is worth mentioning. Am using windows 10
Bill Sanderson on June 9, 2020
This fix error on my mac Thanks - rupaklama on Nov. 6, 2019
I ran “brew install gdal” on my mac, fixed this issue! The installion takes a while. when creating nearbyshops app
lesgod on June 12, 2020
I spent a lot of hours dealing with this OSError error: [WinError 126]. Are there any other suggestions on how I can get rid of it?
George on June 13, 2020
Hi all, tried the same fix as above and basically am ending with the same os
error: OSError: [WinError 126] The specified module could not be found
.
Could you please let us know if this issue is being approached by the extended team for an resolution?! It’s seems to be an issue for quite a while now(just over a year today…), thanks.
Ricky White RP Team on June 14, 2020
HI lesgod and George. Can you share more information? The full Traceback would be helpful so I can help you debug this code. Thanks
George on June 14, 2020
Hi Ricky, unfortunately i’m in between moving house right now and can’t really use my machine; counting on the others in this group facing the same errors to share that traceback; have you tried to replicate this tut on a Windows machine?!
ankitzin on June 16, 2020
I have macbook air and in this while installing postgis it is getting stuck at downloading in elapsed time 0.00.06. Can any one help why this is happening? i tried this postgis installation so many times on windows so please can any one really help me with setting.py whole structure of file.
mikesult on Aug. 11, 2020
A Mac user who adds another thank you to rupaklama on Nov. 6, 2019 for the suggestion to “brew install gdal”. That resolved the django.core.exceptions.ImproperlyConfigured issue and I was able to complete creation of the nearbyshops app.
thanks
bloydkylen on Aug. 14, 2020
Hello Ricky, In regard to George’s comments, I’m having the same issue. Here is the full trace:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\bloydkn\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\db\models\base.py", line 101, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\db\models\base.py", line 305, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\db\models\options.py", line 203, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\db\__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\db\utils.py", line 202, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\db\utils.py", line 110, in load_backend
return import_module('%s.base' % backend_name)
File "C:\Users\bloydkn\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\db\backends\postgis\base.py", line 6, in <module>
from .features import DatabaseFeatures
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\db\backends\postgis\features.py", line 1, in <module>
from django.contrib.gis.db.backends.base.features import BaseSpatialFeatures
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\db\backends\base\features.py", line 3, in <module>
from django.contrib.gis.db.models import aggregates
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\db\models\__init__.py", line 3, in <module>
import django.contrib.gis.db.models.functions # NOQA
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\db\models\functions.py", line 4, in <module>
from django.contrib.gis.db.models.fields import BaseSpatialField, GeometryField
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\db\models\fields.py", line 3, in <module>
from django.contrib.gis import forms, gdal
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\forms\__init__.py", line 3, in <module>
from .fields import ( # NOQA
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\forms\fields.py", line 2, in <module>
from django.contrib.gis.geos import GEOSException, GEOSGeometry
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\geos\__init__.py", line 5, in <module>
from .collections import ( # NOQA
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\geos\collections.py", line 9, in <module>
from django.contrib.gis.geos.geometry import GEOSGeometry, LinearGeometryMixin
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\geos\geometry.py", line 8, in <module>
from django.contrib.gis import gdal
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\gdal\__init__.py", line 28, in <module>
from django.contrib.gis.gdal.datasource import DataSource
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\gdal\datasource.py", line 39, in <module>
from django.contrib.gis.gdal.driver import Driver
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\gdal\driver.py", line 5, in <module>
from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\gdal\prototypes\ds.py", line 9, in <module>
from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal
File "C:\Users\bloydkn\repos\geoshops\myvenv\lib\site-packages\django\contrib\gis\gdal\libgdal.py", line 47, in <module>
lgdal = CDLL(lib_path)
File "C:\Users\bloydkn\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 364, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found
bloydkylen on Aug. 14, 2020
UPDATE Ricky and George, This worked for me: stackoverflow.com/a/49159195/57952
Ricky White RP Team on Aug. 14, 2020
Glad you got it working bloydkylen :)
Yvonne Wilmot on Sept. 18, 2020
Hi. I had same problem with the GDAL path error - Am using windows 10, Python 3.8.5. I only inserted the 2 lines following the BASE_DIR line below into settings.py. (NOTE: that my QGIS library is 3.10, not 3.12 as in above comments):
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
GEOS_LIBRARY_PATH = "C:/Program Files/QGIS 3.10/bin/geos_c.dll"
GDAL_LIBRARY_PATH = "C:/Program Files/QGIS 3.10/bin/gdal300.dll"
Don’t know if you actually need the GEOS_LIBRARY_PATH line as the libgdal.py module only looks for the GDAL_LIBRARY_PATH?
There is most likely a better place to insert this path in settings.py as the comment in settings.py reads ‘# build paths inside the project like this: BASE_DIR / ‘subdir’.
But I have no idea where in this module to put it as I’m still quite new to Python. Under TEMPLATES - DIRS or APP_DIRS perhaps (only place I can see paths/dirs specified)?
steliosioannidesrisk on Oct. 13, 2020
Hi Jackie have we found the solution for the windows environment ? thank you stelios
steliosioannidesrisk on Oct. 13, 2020
thanks everyone. UPDATE Ricky, George ET AL, This worked for me: stackoverflow.com/a/49159195/57952 ALSO !!
you need to make sure that you follow the solution as per link but make sure that you put the additional settings.py part at the top of the said file.
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']
then all commands work! e.g. python manage.py startapp nearbyshops
rosskukard on Jan. 20, 2021
Hi all,
I was getting the same django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (t ried “gdal203”, “gdal202”, “gdal201”, “gdal20”, “gdal111”). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.
error.
Because there seems to be a huge amount of variation here, I will be very specific with the details on how I resolved this.
Date: 20-Jan-2021. OS: Windows 10, 64 bit. Python: v3.9.0. QGIS: 3.16
I did NOT install gdal or any of the other drivers separately, everything was done following the video, except for using the latest versions. As such I did not have an OSGEO4W directory. Instead all of these files are in the QGIS directory.
Using the excellent suggestions above, I added the following code to my settings.py file, right below ALLOWED_HOSTS = []
:
import os
if os.name == 'nt':
import platform
QGIS = r"C:\Program Files\QGIS 3.16"
assert os.path.isdir(QGIS), "Directory does not exist: " + QGIS
os.environ['OSGEO4W_ROOT'] = QGIS
os.environ['GDAL_DATA'] = QGIS + r"\share\gdal"
os.environ['PROJ_LIB'] = QGIS + r"\share\proj"
os.environ['PATH'] = QGIS + r"\bin;" + os.environ['PATH']
After that the python manage.py startapp nearbyshops
command works fine, and python manage.py check
returns System check identified no issues (0 silenced).
chioscar on Feb. 3, 2021
Ok so I am building this via Docker and I am getting the same error - could not find GDAL library
If I manually add the path for it to the file it says,
OSError: /code/C:\Program Files\QGIS 3.16\bin\gdal301.dll: cannot open shared object file: No such file or directory
I added that line after the allowed_hosts = [] and still, nothing. Not sure what is happening here.
chioscar on Feb. 3, 2021
Nvm, realized there’s a whole documentation on QGIS for containerized deployment.
amicewong on Feb. 18, 2021
I am using Mac and got this error when trying to startapp:
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: dlsym(0x7ff31d441a70, OGR_F_GetFieldAsInteger64): symbol not found
Please help. Thanks.
amicewong on Feb. 20, 2021
I refer to my previous questions. and have tried different method but no clue. Since “OGR_F_GetFieldAsInteger64” is raised from the following ds.py, I simple comment that line of code. Now at least “python manage.py runserver” works. Will see what will happen next.
site-packages/django/contrib/gis/gdal/prototypes/ds.py”, line 71, in <module> get_field_as_integer64 = int64_output(lgdal.OGR_F_GetFieldAsInteger64, [c_void_p, c_int])
Orephia on Sept. 2, 2021
Hello, I hit the same wall as everyone above howere unlike everyone else I get this error : django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named ‘psycopg2._psycopg’ I don’t know how to fix it PS: I’m using windows 10 64bits and Python 3.8.7
Bartosz Zaczyński RP Team on Sept. 3, 2021
@Orephia The psycopg2
library is notoriously known to be problematic in installation because of the compilation process that it requires. Have you tried installing pre-built binaries for your specific platform?
$ python -m pip install psycopg2-binary
Orephia on Sept. 3, 2021
@Bartosz when I try this command in my venv I get this error: Failed building wheel for psycopg2-binary, so I just copied the package into the venv
Bartosz Zaczyński RP Team on Sept. 3, 2021
@Orephia There can be many reasons for failure, which can be hard to tell without seeing your specific error message. It should contain some clues as to what went wrong. Do you want to share it?
Orephia on Sept. 3, 2021
@Bartosz here is the full message i get when I try installing it:
PS C:\Users\JIHANE\PycharmProjects\geoApp> pip install psycopg2-binary
WARNING: Ignoring invalid distribution -ip (c:\users\jihane\pycharmprojects\geoapp\venv\lib\python3.8\site-packages)
WARNING: Ignoring invalid distribution -ip (c:\users\jihane\pycharmprojects\geoapp\venv\lib\python3.8\site-packages)
Collecting psycopg2-binary
Using cached psycopg2-binary-2.9.1.tar.gz (380 kB)
Building wheels for collected packages: psycopg2-binary
Building wheel for psycopg2-binary (setup.py) ... error
ERROR: Command errored out with exit status 1:
command: 'c:\users\jihane\pycharmprojects\geoapp\venv\bin\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\JIHANE\\AppDat
a\\Local\\Temp\\pip-install-pzj0wf0i\\psycopg2-binary_5b64c7c3d4fc40b48190d0f875ff1642\\setup.py'"'"'; __file__='"'"'C:\\Users\\JIHANE\\AppData\\Local\\Temp\\pip-inst
all-pzj0wf0i\\psycopg2-binary_5b64c7c3d4fc40b48190d0f875ff1642\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else i
o.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"
'))' bdist_wheel -d 'C:\Users\JIHANE\AppData\Local\Temp\pip-wheel-8hsv9bs2'
cwd: C:\Users\JIHANE\AppData\Local\Temp\pip-install-pzj0wf0i\psycopg2-binary_5b64c7c3d4fc40b48190d0f875ff1642\
Complete output (48 lines):
running bdist_wheel
running build
running build_py
creating build
creating build\lib.mingw-3.8
creating build\lib.mingw-3.8\psycopg2
copying lib\errorcodes.py -> build\lib.mingw-3.8\psycopg2
copying lib\errors.py -> build\lib.mingw-3.8\psycopg2
copying lib\extensions.py -> build\lib.mingw-3.8\psycopg2
copying lib\extras.py -> build\lib.mingw-3.8\psycopg2
copying lib\pool.py -> build\lib.mingw-3.8\psycopg2
copying lib\sql.py -> build\lib.mingw-3.8\psycopg2
copying lib\tz.py -> build\lib.mingw-3.8\psycopg2
copying lib\_ipaddress.py -> build\lib.mingw-3.8\psycopg2
copying lib\_json.py -> build\lib.mingw-3.8\psycopg2
copying lib\_range.py -> build\lib.mingw-3.8\psycopg2
copying lib\__init__.py -> build\lib.mingw-3.8\psycopg2
running build_ext
building 'psycopg2._psycopg' extension
creating build\temp.mingw-3.8
creating build\temp.mingw-3.8\psycopg
C:\msys64\mingw64\bin\x86_64-w64-mingw32-gcc.exe -Wno-unused-result -Wsign-compare -DNDEBUG -march=x86-64 -mtune=generic -O2 -pipe -fwrapv -D__USE_MINGW_ANSI_STDIO=
1 -D_WIN32_WINNT=0x0601 -DNDEBUG -march=x86-64 -mtune=generic -O2 -pipe -fwrapv -D__USE_MINGW_ANSI_STDIO=1 -D_WIN32_WINNT=0x0601 -DNDEBUG "-DPSYCOPG_VERSION=2.9.1 (dt
dec pq3 ext lo64)" -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=100018 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -IC:\users\jihane\pycharmprojects\geoapp\venv\include -IC:\msys64\mingw6
4\include\python3.8 -I. -IC:/PROGRA~1/POSTGR~1/10/include -IC:/PROGRA~1/POSTGR~1/10/include/server -c psycopg\psycopgmodule.c -o build\temp.mingw-3.8\psycopg\psycopgm
odule.o -O3 -fno-strict-aliasing
In file included from ./psycopg/config.h:164,
from ./psycopg/psycopg.h:38,
from psycopg\psycopgmodule.c:28:
./psycopg/win32_support.h:43:6: error: #elif with no expression
43 | #elif
| ^
In file included from ./psycopg/psycopg.h:38,
from psycopg\psycopgmodule.c:28:
psycopg\psycopgmodule.c: In function 'psyco_connect':
./psycopg/config.h:58:49: warning: implicit declaration of function 'getpid'; did you mean 'getcwd'? [-Wimplicit-function-declaration]
58 | fprintf(stderr, "[%d] " fmt "\n", (int) getpid() , ## args)
| ^~~~~~
psycopg\psycopgmodule.c:95:5: note: in expansion of macro 'Dprintf'
95 | Dprintf("psyco_connect: dsn = '%s', async = %d", dsn, async);
| ^~~~~~~
It appears you are missing some prerequisite to build the package from source.
You may install a binary package by installing 'psycopg2-binary' from PyPI.
If you want to install psycopg2 from source, please install the packages
required for the build and try again.
For further information please check the 'doc/src/install.rst' file (also at
<https://www.psycopg.org/docs/install.html>).
error: command 'C:\\msys64\\mingw64\\bin\\x86_64-w64-mingw32-gcc.exe' failed with exit status 1
----------------------------------------
ERROR: Failed building wheel for psycopg2-binary
Running setup.py clean for psycopg2-binary
Failed to build psycopg2-binary
WARNING: Ignoring invalid distribution -ip (c:\users\jihane\pycharmprojects\geoapp\venv\lib\python3.8\site-packages)
Installing collected packages: psycopg2-binary
Running setup.py install for psycopg2-binary ... error
ERROR: Command errored out with exit status 1:
command: 'c:\users\jihane\pycharmprojects\geoapp\venv\bin\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\JIHANE\\AppD
ata\\Local\\Temp\\pip-install-pzj0wf0i\\psycopg2-binary_5b64c7c3d4fc40b48190d0f875ff1642\\setup.py'"'"'; __file__='"'"'C:\\Users\\JIHANE\\AppData\\Local\\Temp\\pip-in
stall-pzj0wf0i\\psycopg2-binary_5b64c7c3d4fc40b48190d0f875ff1642\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else
io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"
'"'))' install --record 'C:\Users\JIHANE\AppData\Local\Temp\pip-record-dyc2t1x3\install-record.txt' --single-version-externally-managed --compile --install-headers 'C
:\users\jihane\pycharmprojects\geoapp\venv\include\site\python3.8\psycopg2-binary'
cwd: C:\Users\JIHANE\AppData\Local\Temp\pip-install-pzj0wf0i\psycopg2-binary_5b64c7c3d4fc40b48190d0f875ff1642\
Complete output (48 lines):
running install
running build
running build_py
creating build
creating build\lib.mingw-3.8
creating build\lib.mingw-3.8\psycopg2
copying lib\errorcodes.py -> build\lib.mingw-3.8\psycopg2
copying lib\errors.py -> build\lib.mingw-3.8\psycopg2
copying lib\extensions.py -> build\lib.mingw-3.8\psycopg2
copying lib\extras.py -> build\lib.mingw-3.8\psycopg2
copying lib\pool.py -> build\lib.mingw-3.8\psycopg2
copying lib\sql.py -> build\lib.mingw-3.8\psycopg2
copying lib\tz.py -> build\lib.mingw-3.8\psycopg2
copying lib\_ipaddress.py -> build\lib.mingw-3.8\psycopg2
copying lib\_json.py -> build\lib.mingw-3.8\psycopg2
copying lib\_range.py -> build\lib.mingw-3.8\psycopg2
copying lib\__init__.py -> build\lib.mingw-3.8\psycopg2
running build_ext
building 'psycopg2._psycopg' extension
creating build\temp.mingw-3.8
creating build\temp.mingw-3.8\psycopg
C:\msys64\mingw64\bin\x86_64-w64-mingw32-gcc.exe -Wno-unused-result -Wsign-compare -DNDEBUG -march=x86-64 -mtune=generic -O2 -pipe -fwrapv -D__USE_MINGW_ANSI_STDI
O=1 -D_WIN32_WINNT=0x0601 -DNDEBUG -march=x86-64 -mtune=generic -O2 -pipe -fwrapv -D__USE_MINGW_ANSI_STDIO=1 -D_WIN32_WINNT=0x0601 -DNDEBUG "-DPSYCOPG_VERSION=2.9.1 (
dt dec pq3 ext lo64)" -DPSYCOPG_DEBUG=1 -DPG_VERSION_NUM=100018 -DHAVE_LO64=1 -DPSYCOPG_DEBUG=1 -IC:\users\jihane\pycharmprojects\geoapp\venv\include -IC:\msys64\ming
w64\include\python3.8 -I. -IC:/PROGRA~1/POSTGR~1/10/include -IC:/PROGRA~1/POSTGR~1/10/include/server -c psycopg\psycopgmodule.c -o build\temp.mingw-3.8\psycopg\psycop
gmodule.o -O3 -fno-strict-aliasing
In file included from ./psycopg/config.h:164,
from ./psycopg/psycopg.h:38,
from psycopg\psycopgmodule.c:28:
./psycopg/win32_support.h:43:6: error: #elif with no expression
43 | #elif
| ^
In file included from ./psycopg/psycopg.h:38,
from psycopg\psycopgmodule.c:28:
psycopg\psycopgmodule.c: In function 'psyco_connect':
./psycopg/config.h:58:49: warning: implicit declaration of function 'getpid'; did you mean 'getcwd'? [-Wimplicit-function-declaration]
58 | fprintf(stderr, "[%d] " fmt "\n", (int) getpid() , ## args)
| ^~~~~~
psycopg\psycopgmodule.c:95:5: note: in expansion of macro 'Dprintf'
95 | Dprintf("psyco_connect: dsn = '%s', async = %d", dsn, async);
| ^~~~~~~
It appears you are missing some prerequisite to build the package from source.
You may install a binary package by installing 'psycopg2-binary' from PyPI.
If you want to install psycopg2 from source, please install the packages
required for the build and try again.
For further information please check the 'doc/src/install.rst' file (also at
<https://www.psycopg.org/docs/install.html>).
error: command 'C:\\msys64\\mingw64\\bin\\x86_64-w64-mingw32-gcc.exe' failed with exit status 1
----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\jihane\pycharmprojects\geoapp\venv\bin\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[
0] = '"'"'C:\\Users\\JIHANE\\AppData\\Local\\Temp\\pip-install-pzj0wf0i\\psycopg2-binary_5b64c7c3d4fc40b48190d0f875ff1642\\setup.py'"'"'; __file__='"'"'C:\\Users\\JIH
ANE\\AppData\\Local\\Temp\\pip-install-pzj0wf0i\\psycopg2-binary_5b64c7c3d4fc40b48190d0f875ff1642\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__)
if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(co
mpile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\JIHANE\AppData\Local\Temp\pip-record-dyc2t1x3\install-record.txt' --single-version-externally-manag
ed --compile --install-headers 'C:\users\jihane\pycharmprojects\geoapp\venv\include\site\python3.8\psycopg2-binary' Check the logs for full command output.
WARNING: Ignoring invalid distribution -ip (c:\users\jihane\pycharmprojects\geoapp\venv\lib\python3.8\site-packages)
WARNING: Ignoring invalid distribution -ip (c:\users\jihane\pycharmprojects\geoapp\venv\lib\python3.8\site-packages)
Bartosz Zaczyński RP Team on Sept. 6, 2021
@Orephia It appears that there’s no prebuilt binary available for your platform, and the pip
command resorts to trying to compile the library from source code using the Mingw compiler. The error message suggests there might be some missing prerequisites and links to the official documentation at www.psycopg.org/docs/install.html for more detail. I’m afraid I can’t help you more with this. Sorry…
Kale OHara on July 26, 2022
Hey Y’all, I’m running a MacOS Monterey (12.2.1) and am getting the “ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried “gdal”, “GDAL”, “gdal3.3.0”, “gdal3.2.0”, “gdal3.1.0”, “gdal3.0.0”, “gdal2.4.0”, “gdal2.3.0”, “gdal2.2.0”, “gdal2.1.0”, “gdal2.0.0”). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings” error that everyone hits. Qgis can run with the latest python3 version so I’m not sure why it’s still hitting this wall. I tried the “brew install gdal” and it installs fine but doesn’t fix the problem. This error message pops up when I try to run “python manage.py runserver”.
Martin Breuss RP Team on July 28, 2022
Kale OHara did you install also the gdal
Python package:
(venv) $ python -m pip install gdal
Make sure that you’ve done this in your virtual environment, and that you can confirm that the package is accessible in the same environment as your Django app.
If you’re still running into the same error, maybe one of these answers can help?
Dee82 on Nov. 8, 2023
Ran into the same error on windows 10 with QGIS 3.28.12 with the GDAL dll not being found. After several hours of searching for options, trial and error , I was finally able to resolve it using a combination of what dthomas01 posted and this post:stackoverflow.com/questions/71847139/solution-to-installing-gdal-proj-geos-in-windows-10-for-django-geodjango
Thanks for posting dthomas01, that helped put me on the right path!!
johnlukow on March 3, 2024
I’m struggling with this tutorial. I got the nearbyshops app working by adding: GDAL_LIBRARY_PATH = r’C:\Program Files\QGIS 3.36.0\bin\gdal308.dll’ and changing ENGINE’: ‘django.db.backends.postgresql’ in settings.py. Hopefully I can keep moving now.
Thierry Gagné on Oct. 2, 2024
I have also ran into the common problem that my program could not find the GDAL library (on a Mac). Running “brew install gdal” also solved my problem.
I am just leaving a comment to highlight that having to waste an hour to understand a problem has left me with a negative impression of this website.
I have loved the content so far, but it makes absolutely no sense to not re-do this content if people have been have the same problems for five years! Now, I don’t know if downloading QGIS was the right method or if I could have done something much simpler.
Martin Breuss RP Team on Oct. 4, 2024
@Thierry Gagné thanks for mentioning, and for bumping this course back up on our priorities list for updates. You’re right that we should have already tackled updates for this course a while ago 😳
We’re actively working on getting older and outdated content up to date, but it takes some juggling to continue creating new content at the same time. Anyways, I’ll make sure to get this course slated for an update as soon as possible.
Thanks everyone for commenting and providing solutions!
Become a Member to join the conversation.
Kevin M on May 12, 2019
I’ve got some issues when trying to create the app. It raised the error (django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried “gdal203”, “gdal202”, “gdal201”, “gdal20”, “gdal111”). Is GDAL installed? If it is, try setting GDAL_LIBR ARY_PATH in your settings. )
but when I set the GDAL_LIBRARY_PATH, it still raised the error. I’m working on Windows