I am creating a library named mygeo
that depends on a GDAL python package.
To install the gdal python library, we must specify the same version of GDAL that is installed on the system, like as follows:
ref: https://gdal.org/api/python_bindings.html#pip
pip install gdal[numpy]=="$(gdal-config --version).*"
I tried to use a tool.setuptools.cmdclass
feature with following pyproject.toml
.
[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "mygeo"
version = "0.1.0"
description = ""
authors = [
{ name = "me", email = "[email protected]" }
]
dependencies = [
# It works if I hard code version of the GDAL here.
# gdal==3.5.1
# other dependencies come here..
]
readme = "README.md"
requires-python = ">= 3.8"
[tool.setuptools]
py-modules=["_custom_build"]
include-package-data = true
[tool.setuptools.cmdclass]
install="_custom_build.install"
The above pyproject.toml
run scripts in install.run()
in _suctom_build.py
.
import sys
import subprocess
from setuptools.command.install import install as _install
class install(_install):
def run(self):
# get gdal version
gdal_version = (
subprocess.check_output(["gdal-config", "--version"]).decode().strip()
)
# Install gdal with pip install
subprocess.check_call(
[sys.executable, "-m", "pip", "install", f"gdal=={gdal_version}"]
)
_install.run(self)
I confirmed that subprocess to get gdal version is running when user pip install .
in the project directory.
But, following subprocess to install gdal with pip did not work.
The error message said that subprocess.CalledProcessError: Command '['/home/takahisa/Documents/git/mygeo/.venv/bin/python3', '-m', 'pip', 'install', 'gdal==3.8.5']' returned non-zero exit status 1.
.
Run pip install gdal==3.8.5
in .venv
manually, or hard code the version on pyproject.toml
.
Is it possible to use the cmdclass
functionality to automatically install a dependency library of the appropriate version for the user’s environment?
The version of GDAL installed on the system varies from user to user.
Is there any other way to dynamically specify the version obtained by the script gdal-config --version
?