import duckdb fails with 'Encountered an error while initializing the extension' when sys.modules has no __main__ (DetectEnvironment imports __main__ unconditionally)
維護者通常 1 天內回覆
評估
研究方向
The failing call is nb::module_::import_("__main__") inside DuckDBPyConnection::DetectEnvironment() in src/pyconnection.cpp, which only checks for __file__ to detect interactive mode. Start by reading that function and reproducing with del sys.modules["__main__"] followed by import duckdb. Done means the import succeeds without __main__ and the default non-interactive environment is used, with existing tests still passing.
由索引模型根據 Issue 內容生成。
描述
What happens?
import duckdb fails when sys.modules has no __main__ entry, because module initialization calls import __main__ unconditionally:
ImportError: Encountered an error while initializing the extension.
DuckDBPyConnection::DetectEnvironment() (src/pyconnection.cpp) runs nb::module_::import_("__main__") only to check hasattr(__main__, "__file__") for interactive-mode detection. If __main__ is absent from sys.modules (and not importable from sys.path), the exception propagates and the whole extension fails to initialize.
This happens in practice on Azure Functions with Python 3.13+: the new Python worker removes sys.modules['__main__'] while reordering sys.path (reported upstream as Azure/azure-functions-python-worker#1958), so any app that imports duckdb fails to start there. Embedded interpreters and other hosts that manipulate sys.modules can hit the same path.
Suggested fix: treat a failed import __main__ as "not interactive" (catch the import error and return the default environment), e.g.:
nb::object main_module;
try {
main_module = nb::module_::import_("__main__");
} catch (nb::python_error &) {
return; // no __main__: not an interactive session
}
To Reproduce
import sys
del sys.modules["__main__"]
import duckdb # ImportError: Encountered an error while initializing the extension.
Run with python -c '...' or as a script; the same snippet without the del line imports fine.
Observed traceback:
Traceback (most recent call last):
File "<string>", line 4, in <module>
import duckdb
File ".../site-packages/duckdb/__init__.py", line 12, in <module>
import _duckdb
ImportError: Encountered an error while initializing the extension.
Also reproduced with duckdb 1.5.4 on Python 3.13 (ImportError: initialization failed), so it is not specific to 2.x.
OS:
macOS 27.2 (arm64) for the minimal repro; Linux x86_64 (Azure Functions Flex Consumption, Python 3.14) in production.
DuckDB Package Version:
2.0.1 (also 1.5.4)
Python Version:
3.14.10 (also 3.13.18)
Full Name / Affiliation:
See the GitHub profile of the reporter.
What is the latest build you tested with?
Latest release on PyPI (2.0.1). The same code is present on main of duckdb/duckdb-python as of 2026-11-25.
Did you include all relevant data sets / code / configuration?
Yes: the repro above is self-contained, with no data and no configuration.
- 主要語言
- Python
- 星號
- 206
- 分支
- 126
- 平均合併
- 20 小時 18 分鐘
- 30 天內合併 PR
- 36
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
duckdb/duckdb-python 的其他 Issue
-
needs triage
難度 2/5 1-3 小時 新手友好度 77/100
duckdb/duckdb-python#752 ·
維護者通常 1 天內回覆
-
`read_json` accepts a list of paths at runtime but the type stub only allows a single path.可能已有人在做 @aleks-drozy 於 118 天前認領。 未關閉needs triage
難度 2/5 1-3 小時 新手友好度 78/100
duckdb/duckdb-python#576 · 1 則留言 ·
維護者通常 1 天內回覆
-
Missing ROW_GROUPS_PER_FILE argument in write_parquet function可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉needs triage
難度 2/5 1-3 小時 新手友好度 68/100
duckdb/duckdb-python#386 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 42/100
duckdb/duckdb-python#789 ·
維護者通常 1 天內回覆
-
needs triage
難度 4/5 3-5 天 新手友好度 55/100
duckdb/duckdb-python#768 ·
維護者通常 1 天內回覆
查看 duckdb/duckdb-python 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 82/100
GanyuanRan/Aegis#112 ·
-
defect from-review
難度 2/5 1-3 小時 新手友好度 86/100
khuisman/mcp-gee-sweet#1669 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 90/100
marimo-team/marimo#12153 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
can4hou6joeng4/boss-agent-cli#3290 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆