No module named '_gdal_array' only on virtual env

Asked by Michael

I've also asked this question on gis stackexchange with no answer: https://gis.stackexchange.com/questions/396207/python-no-module-named-gdal-array-only-on-virtual-env

I'm trying to import gdal_array in python but I get ModuleNotFoundError: No module named '_gdal_array'.

The strange thing is that this problem does not occur if I use system's python 3.8. It only occurs inside python3.8 virtual environment.

Please refer to the link above to check the code, as I cannot paste code here.

I would appreciate it if someone could help me fix the problem.

Thank you.

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu gdal Edit question
Assignee:
No assignee Edit question
Solved by:
Michael
Solved:
Last query:
Last reply:
Revision history for this message
Manfred Hampl (m-hampl) said :
#1

Which Ubuntu release are you running?

If you are using pip to install gdal, then you are wrong here, because this is the question area for the gdal package from the Ubuntu repositories, to be installed with "apt install gdal-bin" or similar commands. This area does not offer support for software installed with pip.

Remark: If you are using python3, then you most probably have to use pip3 instead of pip.

Revision history for this message
Michael (michael5252) said :
#2

Thank you for the response.
I'm using Ubuntu 20.04

I've used the command "sudo apt-get install gdal-bin" and then I installed the python package using
"pip install gdal" so I'm not sure if my issue qualifies in the support category of ubuntu repositories.

To be honest I'm a little confused with gdal installation.

I've already tried your pip3 suggestion with no results (I guess in virtual environment using pip or pip3 is the same).

What I also tried, and maybe you can help me here, is to download a more recent version of gdal (Since the latest version for Ubuntu 20 is 3.0.4)

In order to do that, I've added the repository ppa:ubuntugis/ubuntugis-unstable
and then sudo apt-get install gdal-bin

However, when I run ogrinfo --version I get
free(): invalid pointer
Aborted (core dumped)

Revision history for this message
Manfred Hampl (m-hampl) said :
#3

If you have installed both the Ubuntu version ("sudo apt-get install gdal-bin") and the version from the repositories for pip ("pip install gdal"), then you have potentially conflicting versions installed.

My suggestion:
Remove everything related to gdal (and eventually also numpy and osgeo) and stick to one single source.

If you use the Ubuntu version, then you will also need to install the package python3-gdal

Revision history for this message
Michael (michael5252) said :
#4

From your answer I understand that there are two ways to use gdal in python

1) sudo apt-get install gdal-bin (along with python3-gdal)
2) pip install gdal

However, on this tutorial, https://mothergeo-py.readthedocs.io/en/latest/development/how-to/gdal-ubuntu-pkg.html, it clearly states to execute both 1) and 2).

I tried deleting everything as you said, then executed:
1) sudo apt-get install gdal-bin
2) sudo apt-get install python3-gdal

Then on terminal I typed python3.8 and executed a command but it seems like gdal python package was still not installed. I still needed to run "pip install gdal".

It seems like I might need to format my computer !

Revision history for this message
Manfred Hampl (m-hampl) said :
#5

To be honest, I do not have experience of my own with mixing gdal from different sources, but I have seen some cases where mixing different sources dit not work at all. Maybe gdal is different and it works, but I do not expect that.

"Then on terminal I typed python3.8 and executed a command but it seems like gdal python package was still not installed. "
What happened? Was there any meaningful error message?

Remark: there is a PPA with a higher version of gdal for Ubuntu 20.04, https://launchpad.net/~osgeolive/+archive/ubuntu/release-14.0

Revision history for this message
Michael (michael5252) said :
#6

The issue was fixed by rebuilding the wheel for gdal.
All this time, when I pip installed gdal, it used the cached version of the wheel, which it seems like it was problematic.

Fortunately I deleted my .cache folder earlier today to free up some space and it turns out that I forced pip to rebuild the wheel for gdal, and this fixed the problem.

Thank you for your time anyway, I appreciate it !