From 907a5ab3f3b9268e60632b71d6626b3c6f9966b1 Mon Sep 17 00:00:00 2001 From: Kayvan Zahiri Date: Thu, 24 Sep 2026 17:13:33 -0700 Subject: [PATCH] Make scipy optional again and declare packaging Since seekpath/__init__.py started importing brillouinzone (2024), scipy, which is only the `bz` extra, became required for `import seekpath`. Import scipy inside BZ._initialize_BZ instead. hpkot/tools.py also imports packaging, which was never declared, so get_path failed on a clean install. --- pyproject.toml | 1 + seekpath/brillouinzone/brillouinzone.py | 11 +++++++++-- tests/test_import_without_scipy.py | 10 ++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 tests/test_import_without_scipy.py diff --git a/pyproject.toml b/pyproject.toml index 4cf4ba4..ee903c4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,7 @@ classifiers = [ requires-python = ">=3.8" dependencies = [ "numpy>=1.0", + "packaging", "spglib>=1.14.1", ] license = {text = "The MIT license"} diff --git a/seekpath/brillouinzone/brillouinzone.py b/seekpath/brillouinzone/brillouinzone.py index 8bd93b9..2374e29 100644 --- a/seekpath/brillouinzone/brillouinzone.py +++ b/seekpath/brillouinzone/brillouinzone.py @@ -1,11 +1,15 @@ """Module to compute the Brillouin zone of a crystal.""" +from __future__ import annotations + from collections import defaultdict import warnings -from typing import Union +from typing import TYPE_CHECKING, Union import numpy as np -from scipy.spatial import Voronoi, ConvexHull, Delaunay + +if TYPE_CHECKING: + from scipy.spatial import ConvexHull, Delaunay def get_BZ( @@ -127,6 +131,9 @@ def _initialize_BZ( plotting library prefers these - these are not oriented for the time being) """ + # scipy is the optional `bz` extra, so import it only when a BZ is built. + from scipy.spatial import ConvexHull, Delaunay, Voronoi + ret_data = {} supercell_size = 3 # Is this enough? diff --git a/tests/test_import_without_scipy.py b/tests/test_import_without_scipy.py new file mode 100644 index 0000000..a5f1932 --- /dev/null +++ b/tests/test_import_without_scipy.py @@ -0,0 +1,10 @@ +"""scipy is only needed for the Brillouin zone (the `bz` extra).""" + +import subprocess +import sys + + +def test_import_without_scipy(): + """`import seekpath` must work when scipy is not installed.""" + code = "import sys; sys.modules['scipy'] = None; import seekpath" + subprocess.run([sys.executable, '-c', code], check=True)