Skip to content

Fix Open3D import crash when pybind stub directories exist (#7481)#7510

Open
Iyanuoluwa007 wants to merge 1 commit into
isl-org:mainfrom
Iyanuoluwa007:fix-pybind-glob-directory
Open

Fix Open3D import crash when pybind stub directories exist (#7481)#7510
Iyanuoluwa007 wants to merge 1 commit into
isl-org:mainfrom
Iyanuoluwa007:fix-pybind-glob-directory

Conversation

@Iyanuoluwa007

@Iyanuoluwa007 Iyanuoluwa007 commented Jun 10, 2026

Copy link
Copy Markdown

Type

Fixes #7481

Related issue:
#7481

  • Bug fix (non-breaking change which fixes an issue): Fixes #
  • New feature (non-breaking change which adds functionality). Resolves #
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) Resolves #

Motivation and Context

Checklist:

  • I have run python util/check_style.py --apply to apply Open3D code style
    to my code.
  • This PR changes Open3D behavior or adds new functionality.
    • Both C++ (Doxygen) and Python (Sphinx / Google style) documentation is
      updated accordingly.
    • I have added or updated C++ and / or Python unit tests OR included test
      results
      (e.g. screenshots or numbers) here.
  • I will follow up and update the code if CI fails.
  • For fork PRs, I have selected Allow edits from maintainers.

Description

Problem

When generating Python stubs using pybind11-stubgen, a pybind/ directory is created inside the Open3D package (e.g. open3d/cpu/pybind/ or open3d/cuda/pybind/).

The current implementation in open3d/__init__.py locates the compiled Python extension using:

CDLL(str(next(Path(...).glob("pybind*"))))

However, glob("pybind*") may return both:

  • a directory (pybind/)
  • a compiled shared library (pybind*.so, pybind*.dll, etc.)

Because next() does not filter by type, the directory may be selected first.

This leads to a runtime crash when ctypes attempts to load a directory:

OSError: ... pybind: Is a directory

Root Cause

  • Path.glob("pybind*") matches both files and directories
  • No filtering is applied before passing the result to CDLL
  • Directory entries can be returned before the actual shared library depending on filesystem ordering

Fix

Filter glob results to ensure only valid shared library files are used:

p for p in Path(...).glob("pybind*") if p.is_file()

## Testing

Reproduced the issue described in #7481 by simulating the directory layout
created by `pybind11-stubgen`.

Created:

cpu/
├── pybind/      (directory)
└── pybind.so    (shared library)

Test script:

```python
from pathlib import Path

paths = list(Path("cpu").glob("pybind*"))
print("ALL MATCHES:", paths)

filtered = [p for p in paths if p.is_file()]
print("FILES ONLY:", filtered)

selected = next(
    p for p in Path("cpu").glob("pybind*")
    if p.is_file()
)
print("SELECTED FILE:", selected)

@update-docs

update-docs Bot commented Jun 10, 2026

Copy link
Copy Markdown

Thanks for submitting this pull request! The maintainers of this repository would appreciate if you could update the CHANGELOG.md based on your changes.

@ssheorey ssheorey requested a review from Copilot June 18, 2026 21:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Fixes a runtime crash in Open3D’s Python import path when pybind11-stubgen (or similar tooling) leaves pybind/ stub directories alongside the compiled extension, causing ctypes.CDLL to attempt to load a directory.

Changes:

  • Filter Path.glob("pybind*") results to files-only before passing to ctypes.CDLL (CPU path).
  • Apply the same files-only filtering for the CUDA ctypes.CDLL load used during CUDA availability checks.

Comment thread python/open3d/__init__.py
Comment on lines +39 to +42
CDLL(str(next(
p for p in (Path(__file__).parent / "cpu").glob("pybind*")
if p.is_file()
)))
Comment thread python/open3d/__init__.py
Comment on lines +67 to +70
_pybind_cuda = CDLL(str(next(
p for p in (Path(__file__).parent / "cuda").glob("pybind*")
if p.is_file()
)))
Comment thread python/open3d/__init__.py
Comment on lines +39 to 43
CDLL(str(next(
p for p in (Path(__file__).parent / "cpu").glob("pybind*")
if p.is_file()
)))
except StopIteration:
Comment thread python/open3d/__init__.py
Comment on lines +39 to +42
CDLL(str(next(
p for p in (Path(__file__).parent / "cpu").glob("pybind*")
if p.is_file()
)))
Comment thread python/open3d/__init__.py
Comment on lines +67 to +70
_pybind_cuda = CDLL(str(next(
p for p in (Path(__file__).parent / "cuda").glob("pybind*")
if p.is_file()
)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Official pybind11-stubgen instructions break Open3D's runtime import

3 participants