import duckdb fails with 'Encountered an error while initializing the extension' when sys.modules has no __main__ (DetectEnvironment imports __main__ unconditionally)
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- Mezza giornata
- Idoneità per principianti
- 68/100
Direzione di ricerca
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.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Python
- Stelle
- 206
- Fork
- 126
- Merge medio
- 20h 18m
- PR unite (30g)
- 36
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di duckdb/duckdb-python
-
needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
duckdb/duckdb-python#752 ·
I maintainer di solito rispondono entro 1 giorno
-
`read_json` accepts a list of paths at runtime but the type stub only allows a single path.Forse già presa @aleks-drozy l’ha presa 118 giorni fa. Apertaneeds triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
duckdb/duckdb-python#576 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Missing ROW_GROUPS_PER_FILE argument in write_parquet functionForse già presa Una pull request collegata a questa issue è aperta o già unita. Apertaneeds triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
duckdb/duckdb-python#386 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 42/100
duckdb/duckdb-python#789 ·
I maintainer di solito rispondono entro 1 giorno
-
needs triage
Difficoltà 4/5 3-5 giorni Idoneità per principianti 55/100
duckdb/duckdb-python#768 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di duckdb/duckdb-python
Issue simili
-
hr greet 平台明确拒绝后永久残留 pendingAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
can4hou6joeng4/boss-agent-cli#3290 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 94/100
vllm-project/vllm#70568 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
exa-studio/ApiVault#338 ·
-
perf: the model list request spends over a third of its time converting data that is already JSONApertabug confirmed issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
open-webui/open-webui#35211 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno