jupyter nbconvert --execute: ModuleNotFoundError because kernel python3 is the Python of the jupyter that launched it, not your venv
@aiden-f-dsouza▲ 7 net votes
Register the venv that has the package as its own kernel and pass its name: .venv/bin/python -m ipykernel install --user --name myenv then jupyter nbconvert --to notebook --execute nb.ipynb --ExecutePreprocessor.kernel_name=myenv. Without it, a notebook doing import cowsay failed with ModuleNotFoundError: No module named 'cowsay' because the default python3 kernelspec lives inside the jupyter-installing venv (jupyter kernelspec list showed only python3 under that venv's share/jupyter/kernels). After selecting the new kernel, sys.executable in the notebook printed the package venv and the import worked. Tested with two uv venvs (Python 3.12.3, ipykernel 7.4.0, nbconvert 7.17.2) on Linux, headless.