Trying to import gdal from osgeo and I'm getting this error;

ImportError: DLL load failed while importing _gdal: The specified module could not be found.

On Windows, with Python >= 3.8, DLLs are no longer imported from the PATH.
If gdalXXX.dll is in the PATH, then set the USE_PATH_FOR_GDAL_PYTHON=YES environment variable
to feed the PATH into os.add_dll_directory().

I have seen this error referenced in a number of places but no solution that I can see, some suggest using conda or miniconda to make a virtual environment but that isn't an option for me. I just want to be able to use gdal from my QGIS python console and pyCharm console directly.

What does this error mean? How can I set my USE_PATH_FOR_GDAL_PYTHON=YES? Do I use OSGeo4W Shell? Do I need to edit a BAT file? How is os.add_dll_directory() used to feed the PATH? What PATH is it looking for? Am I just importing gdal incorrectly with from osgeo import gdal? Is there another way to import gdal? OS Windows 10, OSGeo4W installed on C:, QGIS 3.22.6, PyCharm 2021.1.1, Python3.9(C:\OSGeo4W\apps\Python39\python.exe)

1 Answer 1


In using this guide https://spatialgalaxy.net/2018/02/13/quick-guide-to-getting-started-with-pyqgis3-on-windows/ I was able to create a bat file that when run loads all the proper paths for

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W :: Change this to OSGeo4w or OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat 
call "%OSGEO4W_ROOT%"\apps\grass\grass-7.4.0\etc\env.bat :: "grass-7.4.0" will need to be replaced with your grass version in ...apps\grass Newer versions use grass78 or grass80
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass-7.4.0\lib :: See note on grass versions
path %PATH%;C:\OSGeo4W3\apps\Qt5\bin
path %PATH%;C:\OSGeo4W3\apps\Python36\Scripts :: Python36 references will need to be changed, newer versions use Python39

set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python36 :: See python note

set PATH=C:\Program Files\Git\bin;%PATH%

cmd.exe :: Delete this line and add 'start "PyCharm aware of QGIS" /B "PATHTOPYCHARM.exe"'

Run the bat file and you should get a PyCharm running with OSGeo4W paths and a working osgeo and gdal, you need to import gdal from osgeo

from osgeo import gdal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.