2

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)

2 Answers 2

-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 PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
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
5
  • That guide is ancient, outdated and should not be used anymore. Commented May 4, 2023 at 19:49
  • <QGIS-dir>\bin\python-qgis[-ltr].bat Commented May 4, 2023 at 19:50
  • But the process works, just needs to be updated for newer versions of QGIS.
    – MrKingsley
    Commented May 23, 2023 at 12:17
  • Better trust the QGIS developers who do exactly that with the <QGIS-dir>\bin\python-qgis[-ltr].bat file! No one needs to manually build or copy-paste these lines anymore. Commented May 23, 2023 at 12:38
  • 2
    Perhaps then you could update the answer with your preferred way of getting Pycharm to work with QGIS.
    – MrKingsley
    Commented May 23, 2023 at 15:06
-1

I took the error message as an instruction and did the following.

Firstly, I tried to add the custom environment variable USE_PATH_FOR_GDAL_PYTHON

environment variable USE_PATH_FOR_GDAL_PYTHON

The error message changed a bit, but it still complained about gdalXXX.dll.

So, I opened the C:\OSGeo4W folder and searched in it for gdal*.dll

OSGeo4W folder search for gdal*.dll

Then I manually copied all these folders paths and added them to my Path.

User Path

Here they are, 4 folders.

OSGeo4W gdal folders in Path

Now from osgeo import gdal works.

from osgeo import gdal

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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