1
2
3
4 from sys import platform, version_info
5 if version_info >= (3, 8, 0) and platform == 'win32':
6 import os
7 if 'USE_PATH_FOR_GDAL_PYTHON' in os.environ and 'PATH' in os.environ:
8 for p in os.environ['PATH'].split(';'):
9 if p:
10 try:
11 os.add_dll_directory(p)
12 except (FileNotFoundError, OSError):
13 continue
14 elif 'PATH' in os.environ:
15 import glob
16 for p in os.environ['PATH'].split(';'):
17 if glob.glob(os.path.join(p, 'gdal*.dll')) or glob.glob(os.path.join(p, 'libgdal*.dll')):
18 try:
19 os.add_dll_directory(p)
20 break
21 except (FileNotFoundError, OSError):
22 continue
23
24
26 import importlib
27 from os.path import dirname, basename
28 mname = basename(dirname(__file__)) + '._gdal'
29 try:
30 return importlib.import_module(mname)
31 except ImportError:
32 if version_info >= (3, 8, 0) and platform == 'win32':
33 import os
34 if not 'USE_PATH_FOR_GDAL_PYTHON' in os.environ:
35 msg = 'On Windows, with Python >= 3.8, DLLs are no longer imported from the PATH.\n'
36 msg += 'If gdalXXX.dll is in the PATH, then set the USE_PATH_FOR_GDAL_PYTHON=YES environment variable\n'
37 msg += 'to feed the PATH into os.add_dll_directory().'
38
39 import sys
40 import traceback
41 traceback_string = ''.join(traceback.format_exception(*sys.exc_info()))
42 raise ImportError(traceback_string + '\n' + msg)
43 return importlib.import_module('_gdal')
44
45
46 _gdal = swig_import_helper()
47 del swig_import_helper
48
49 __version__ = _gdal.__version__ = _gdal.VersionInfo("RELEASE_NAME")
50
51 gdal_version = tuple(int(s) for s in str(__version__).split('.') if s.isdigit())[:3]
52 python_version = tuple(version_info)[:3]
53
54
55
56
57
58
59
60 fail_on_unsupported_version = False
61
62
63
64
65
66
67 gdal_version_and_min_supported_python_version = (
68 ((0, 0), (0, 0)),
69 ((1, 0), (2, 0)),
70 ((2, 0), (2, 7)),
71 ((3, 3), (3, 6)),
72
73
74 )
75
76
78 return '.'.join(str(v) for v in ver) if ver is not None else None
79
80
81 minimum_supported_python_version_for_this_gdal_version = None
82 this_python_version_will_be_deprecated_in_gdal_version = None
83 last_gdal_version_to_supported_your_python_version = None
84 next_version_of_gdal_will_use_python_version = None
85 for gdal_ver, py_ver in gdal_version_and_min_supported_python_version:
86 if gdal_version >= gdal_ver:
87 minimum_supported_python_version_for_this_gdal_version = py_ver
88 if python_version >= py_ver:
89 last_gdal_version_to_supported_your_python_version = gdal_ver
90 if not this_python_version_will_be_deprecated_in_gdal_version:
91 if python_version < py_ver:
92 this_python_version_will_be_deprecated_in_gdal_version = gdal_ver
93 next_version_of_gdal_will_use_python_version = py_ver
94
95
96 if python_version < minimum_supported_python_version_for_this_gdal_version:
97 msg = 'Your Python version is {}, which is no longer supported by GDAL {}. ' \
98 'Please upgrade your Python version to Python >= {}, ' \
99 'or use GDAL <= {}, which supports your Python version.'.\
100 format(ver_str(python_version), ver_str(gdal_version),
101 ver_str(minimum_supported_python_version_for_this_gdal_version),
102 ver_str(last_gdal_version_to_supported_your_python_version))
103
104 if fail_on_unsupported_version:
105 raise Exception(msg)
106 else:
107 from warnings import warn, simplefilter
108 simplefilter('always', DeprecationWarning)
109 warn(msg, DeprecationWarning)
110 elif this_python_version_will_be_deprecated_in_gdal_version:
111 msg = 'You are using Python {} with GDAL {}. ' \
112 'This Python version will be deprecated in GDAL {}. ' \
113 'Please consider upgrading your Python version to Python >= {}, ' \
114 'Which will be the minimum supported Python version of GDAL {}.'.\
115 format(ver_str(python_version), ver_str(gdal_version),
116 ver_str(this_python_version_will_be_deprecated_in_gdal_version),
117 ver_str(next_version_of_gdal_will_use_python_version),
118 ver_str(this_python_version_will_be_deprecated_in_gdal_version))
119
120 from warnings import warn, simplefilter
121 simplefilter('always', DeprecationWarning)
122 warn(msg, DeprecationWarning)
123