Reputation: 723
I am trying to run a Python script test.py to test the gdal library. The script contains the line:
from osgeo import gdal
At that line I get the traceback:
File "~/test.py", line 9, in <module>
from osgeo import gdal
File "/usr/local/lib/python2.7/site-packages/GDAL-2.2.0/osgeo/__init__.py", line 21, in <module>
gdal = swing_import_helper()
File "/usr/local/lib/python2.7/site-packages/GDAL-2.2.0/osgeo/__init__.py", line 13, in swig_import_helper
import gdal
ImportError: No module named _gdal
I don't see any _gdal Python files or any scripts that define _gdal. Do I need to install another (prerequisite) library that defines _gdal? The GDAL package should include everything needed to install the library and run scripts that import gdal.
Upvotes: 16
Views: 26053
Reputation: 763
Along with the other answers here this can also happen if you build and install from sources then attempt to run the test command from the same folder that contains the osgeo python bindings, namely swig/python/
.
By default running python -c 'from osgeo import gdal; print(gdal.__version__)'
from that location will fail.
Since I only wanted to search the installation site-packages folder for the new module the solution was to run python in isolation mode so that the current directory was no longer included:
python -I -c 'from osgeo import gdal; print(gdal.__version__)'
Upvotes: 0
Reputation: 81
Credits: https://udayrage.github.io/geoAnalytics.html
GDAL will often pose problems since there is a very specific process that one has to take to install it. Follow these steps to correctly install GDAL.
Make a new conda environment, then run these.
sudo apt-get update && sudo apt upgrade -y && sudo apt autoremove
sudo apt-get install -y cdo nco gdal-bin libgdal-dev
python -m pip install --upgrade pip setuptools wheel
python -m pip install --upgrade gdal
conda install -c conda-forge libgdal
conda install -c conda-forge gdal
conda install tiledb=2.2
conda install poppler
If installed correctly, this line should output the GDAL version.
ogrinfo --version
Upvotes: 8
Reputation: 2930
I had a couple of issues that both resulted in the "No module named _gdal" error message. First, I'd installed GDAL to a non-standard location and didn't realize that the server also had an ancient version installed from a system package. From ldd _gdal.so
I was able to figure out that the new bindings were built against the old system library, which didn't work. Since I can't modify the system packages, I downloaded the sources from pypi.org and used environment variables to make sure the bindings were built against my preferred GDAL version:
d="/path/to/gdal"
env CPPFLAGS=-I$d/include LDFLAGS="-L$d/lib -Wl,-rpath,$d/lib" python2 setup.py install
After doing that, the _gdal.so file was linked to the correct libgdal, but I still couldn't import it. I got a better error message by using ctypes to try to load it:
>>> ctypes.cdll.LoadLibrary("_gdal.so")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/ctypes/__init__.py", line 438, in LoadLibrary
return self._dlltype(name)
File "/usr/lib64/python2.7/ctypes/__init__.py", line 360, in __init__
self._handle = _dlopen(self._name, mode)
OSError: /lib64/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by /u/pmr0/uavops/Software/spack-0.16.2/var/spack/environments/uavops/.spack-env/view/lib/libgdal.so.28)
which I fixed by compiling the bindings with the same C++ compiler I used to compile libgdal.
Upvotes: 0
Reputation: 866
For using from osgeo import gdal
or installing gdal
for any purpose on windows. You can follow the below methods.
pip list
for that. Mine was GDAL==3.2.3
.python --version
will do the work. Mine it was 3.7
.After this you can go to this link and download the required wheel.
Geospatial Data Abstraction Library is a translator library for raster geospatial data formats
I downloaded GDAL‑3.2.3‑cp37‑cp37m‑win_amd64.whl
.
Now pip install GDAL‑3.2.3‑cp37‑cp37m‑win_amd64.whl
. This will work.
Thanks
Upvotes: 1
Reputation: 1287
As simple as:
ldconfig
or if you need a quick fix from within a python session
os.system('ldconfig')
ldconfig fixes automagically broken symlink where a version has been updated.
Upvotes: 1
Reputation: 170
Since some people still have problems about it (me included), I want to give some clarity:
ImportError: No module named _gdal
refers to the file _gdal.so
. It is a dynamic library that should be automatically created when building gdal from source using python3:
./configure --with-python=python3 && make
However, even though configure
tells me that SWIG python bindings will be used, the make
command won't build them. I think that python bindings have always been a problem with GDAL in general.
Now, if you do not want to install GDAL (make install
), you will have to build the bindings manually: go to /gdal/swig/python
and run python setup.py build
. After that, all the modules and libraries will be created: make sure that paths can be found and everything should work fine, including running tests as the OP wanted to.
If instead you want to install GDAL, what Benedikt wrote is enough to let everything work.
Upvotes: 1
Reputation: 65
I faced the same issue and I resolved it by looking for my osgeo(gdal) install lib path and adding it to the PYTHONPATH
find / -name "_gdal*"
to find your osgeo lib installation pathexport PYTHONPATH=/path/to/lib/python2.7/site-packages/:$PYTHONPATH
# you can also add it permanently to your ~/.bashrc
Upvotes: 1
Reputation: 39
In my case, find the osgeo
directory from site-packages
or dist-packages
in your python lib
directory, and then rename _gdal.cpython-35m-x86_64-linux-gnu.so
as _gdal.so
, other files also like this. Then you can import gdal correctly.
Upvotes: 3
Reputation: 5109
In my case, I had build GDAL from sources (with ./configure --with-python && make
) and then tried to run the Python tools directly from the ./swig/python/build/lib.linux-x86_64-2.7/
source folder. This lead to the same error:
[beni@vendace gdal-2.4.0]$ PYTHONPATH=./swig/python/build/lib.linux-x86_64-2.7/:$PYTHONPATH python -c 'from osgeo import gdal'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/beni/gdal/gdal-2.4.0/swig/python/build/lib.linux-x86_64-2.7/osgeo/__init__.py", line 21, in <module>
_gdal = swig_import_helper()
File "/home/beni/gdal/gdal-2.4.0/swig/python/build/lib.linux-x86_64-2.7/osgeo/__init__.py", line 13, in swig_import_helper
import _gdal
ImportError: No module named _gdal
After installing GDAL (with make install
), the _gdal.so
module is built, and I can now import gdal:
[beni@vendace gdal-2.4.0]$ LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH PYTHONPATH=/usr/local/lib64/python2.7/site-packages/:$PYTHONPATH python -c 'from osgeo import gdal; print(gdal.__version__);'
2.4.0
Upvotes: 1