Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python38/Windows dll load directory #852

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ environment:
GIS_INTERNALS: "release-1911-x64-gdal-2-4-2-mapserver-7-4-0.zip"
GIS_INTERNALS_LIBS: "release-1911-x64-gdal-2-4-2-mapserver-7-4-0-libs.zip"

# Use daily stable branch of gdal 3.0.x series until release with gdal >= 3.0.1 is released
- PYTHON: "C:\\Python36-x64"
PYTHON_VERSION: "3.6"
PYTHON_ARCH: "64"
GDAL_VERSION: "3.0.0"
GIS_INTERNALS: "release-1911-x64-gdal-3-0-mapserver-7-4.zip"
GIS_INTERNALS_LIBS: "release-1911-x64-gdal-3-0-mapserver-7-4-libs.zip"
GDAL_VERSION: "3.0.2"
GIS_INTERNALS: "release-1911-x64-gdal-3-0-2-mapserver-7-4-2.zip"
GIS_INTERNALS_LIBS: "release-1911-x64-gdal-3-0-2-mapserver-7-4-2-libs.zip"
PROJ_LIB: "C:\\gdal\\bin\\proj6\\share"

- PYTHON: "C:\\Python37-x64"
Expand All @@ -57,9 +56,9 @@ environment:
- PYTHON: "C:\\Python37-x64"
PYTHON_VERSION: "3.7"
PYTHON_ARCH: "64"
GDAL_VERSION: "3.0.0"
GIS_INTERNALS: "release-1911-x64-gdal-3-0-mapserver-7-4.zip"
GIS_INTERNALS_LIBS: "release-1911-x64-gdal-3-0-mapserver-7-4-libs.zip"
GDAL_VERSION: "3.0.2"
GIS_INTERNALS: "release-1911-x64-gdal-3-0-2-mapserver-7-4-2.zip"
GIS_INTERNALS_LIBS: "release-1911-x64-gdal-3-0-2-mapserver-7-4-2-libs.zip"
PROJ_LIB: "C:\\gdal\\bin\\proj6\\share"

- PYTHON: "C:\\Python37-x64"
Expand All @@ -70,6 +69,14 @@ environment:
GIS_INTERNALS_LIBS: "release-1911-x64-gdal-3-0-mapserver-7-4-libs.zip"
PROJ_LIB: "C:\\gdal\\bin\\proj6\\share"

- PYTHON: "C:\\Python38-x64"
PYTHON_VERSION: "3.8"
PYTHON_ARCH: "64"
GDAL_VERSION: "3.0.2"
GIS_INTERNALS: "release-1911-x64-gdal-3-0-2-mapserver-7-4-2.zip"
GIS_INTERNALS_LIBS: "release-1911-x64-gdal-3-0-2-mapserver-7-4-2-libs.zip"
PROJ_LIB: "C:\\gdal\\bin\\proj6\\share"

- PYTHON: "C:\\Python38-x64"
PYTHON_VERSION: "3.8"
PYTHON_ARCH: "64"
Expand Down
56 changes: 48 additions & 8 deletions fiona/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,62 @@
import os
import sys
import warnings

import platform
from six import string_types
from collections import OrderedDict

try:
from pathlib import Path
except ImportError: # pragma: no cover
class Path:
pass
from pathlib import Path

# TODO: remove this? Or at least move it, flake8 complains.
if sys.platform == "win32":
libdir = os.path.join(os.path.dirname(__file__), ".libs")
os.environ["PATH"] = os.environ["PATH"] + ";" + libdir

try:

from fiona.ogrext import _bounds, _listlayers, FIELD_TYPES_MAP, _remove, \
_remove_layer

except ImportError as e:
"""
With Python >= 3.8 on Windows directories in PATH are not automatically
searched for DLL dependencies and must be added manually with
os.add_dll_directory.

see https://github.com/Toblerity/Fiona/issues/851

We check if a */gdal/bin directory is found in PATH and
if none is found if GDAL_HOME is set.
"""
if platform.system() == 'Windows' and (3, 8) <= sys.version_info:

dll_directory = None

# Parse PATH for gdal/bin
for path in os.getenv('PATH', '').split(';'):
p = Path(path.lower())

if p.parts[-2:] == ('gdal', 'bin') and os.path.exists(path):
dll_directory = path
break
del path, p

# Use GDAL_HOME if present
if dll_directory is not None:
gdal_home = os.getenv('GDAL_HOME', None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be added to the documentation. I don't think it's a standard runtime environment variable for GDAL, although it does seem to be used in the build guide for Windows.


if gdal_home is not None and os.path.exists(gdal_home):
dll_directory = os.path.join(gdal_home, "bin")
del gdal_home

if dll_directory is not None:
os.add_dll_directory(dll_directory)
del dll_directory

from fiona.ogrext import _bounds, _listlayers, FIELD_TYPES_MAP, \
_remove, _remove_layer

else:
raise e
from fiona.collection import BytesCollection, Collection
from fiona.drvsupport import supported_drivers
from fiona.env import ensure_env_with_credentials, Env
Expand All @@ -91,7 +132,6 @@ class Path:
calc_gdal_version_num, get_gdal_version_num, get_gdal_release_name,
get_gdal_version_tuple)
from fiona.io import MemoryFile
from fiona.ogrext import _bounds, _listlayers, FIELD_TYPES_MAP, _remove, _remove_layer
from fiona.path import ParsedPath, parse_path, vsi_path
from fiona.vfs import parse_paths as vfs_parse_paths

Expand Down