import duckdb fails with 'Encountered an error while initializing the extension' when sys.modules has no __main__ (DetectEnvironment imports __main__ unconditionally)
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 3/5
- 見積もり時間
- 半日
- 初心者へのやさしさ
- 68/100
調査の方向性
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分
- マージ済み PR(30日)
- 36
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン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 日以内に返信