Skip to content

Commit 6378b44

Browse files
committed
Python bindings: honour GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY=YES/1/ON/TRUE or NO/0/OFF/FALSE
Fixes OSGeo#11853
1 parent f18aaa7 commit 6378b44

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

swig/python/setup.py.in

+24-12
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,32 @@ if sys.platform == 'win32':
186186

187187
numpy_include_dir = '.'
188188
numpy_error_msg = ""
189-
try:
190-
numpy_include_dir = get_numpy_include()
191-
HAVE_NUMPY = numpy_include_dir != '.'
189+
190+
do_numpy_detection = True
191+
if "GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY" in os.environ:
192+
v = os.environ["GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY"].upper()
193+
if v in ('YES', '1', 'ON', 'TRUE'):
194+
do_numpy_detection = False
195+
elif v not in ('NO', '0', 'OFF', 'FALSE'):
196+
raise Exception("Unrecognized value for GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY")
197+
198+
if do_numpy_detection:
199+
try:
200+
numpy_include_dir = get_numpy_include()
201+
HAVE_NUMPY = numpy_include_dir != '.'
202+
if not HAVE_NUMPY:
203+
numpy_error_msg = "numpy found, but numpy headers were not found!"
204+
except ImportError:
205+
HAVE_NUMPY = False
206+
numpy_error_msg = "numpy not available!"
207+
192208
if not HAVE_NUMPY:
193-
numpy_error_msg = "numpy found, but numpy headers were not found!"
194-
except ImportError:
209+
if "GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY" in os.environ:
210+
print("WARNING: " + numpy_error_msg + " Array support will not be enabled.")
211+
else:
212+
raise Exception(numpy_error_msg + " This error may happen if you build/install using setup.py directly, but should normally not happen if you install using pip install. If you still want to build the bindings without numpy support, define the GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY environment variable")
213+
else:
195214
HAVE_NUMPY = False
196-
numpy_error_msg = "numpy not available!"
197-
198-
if not HAVE_NUMPY:
199-
if "GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY" in os.environ:
200-
print("WARNING: " + numpy_error_msg + " Array support will not be enabled.")
201-
else:
202-
raise Exception(numpy_error_msg + " This error may happen if you build/install using setup.py directly, but should normally not happen if you install using pip install. If you still want to build the bindings without numpy support, define the GDAL_PYTHON_BINDINGS_WITHOUT_NUMPY environment variable")
203215

204216
class gdal_ext(build_ext):
205217

0 commit comments

Comments
 (0)