ModuleNotFoundError in fresh conda environment

Hi All,

I’m trying to follow the tutorial: nbdev tutorial | nbdev. I like to use conda to have isolated environments.

I created a new conda environment and installed nbdev using:

conda create -n nbdev -c conda-forge jupyterlab
conda activate nbdev
pip install nbdev

I confirm everything is ok by running:

python                                            (nbdev) 22:32:37
Python 3.10.2 | packaged by conda-forge | (main, Feb  1 2022, 19:30:18) [Clang 11.1.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nbdev
>>> nbdev
<module 'nbdev' from '/Users/dleen/miniconda3/envs/nbdev/lib/python3.10/site-packages/nbdev/__init__.py'>
>>>

My repo was created from the template from the tutorial: https://github.com/dleen/nbdevtest.

When I run nbdev_build_docs I get the following error:

ModuleNotFoundError                       Traceback (most recent call last)
/var/folders/lz/jtq8b7qn037d7n39zwlnxk000000gn/T/ipykernel_17420/1418304125.py in <module>
----> 1 from nbdev.showdoc import show_doc

ModuleNotFoundError: No module named 'nbdev'
ModuleNotFoundError: No module named 'nbdev'

An error occurred while executing the following cell:
------------------
from nbdev.showdoc import show_doc
------------------

What I’ve tried:

  • Installing nbdev in a non conda environment: same issue
  • Running the notebook from Jupyter: the import works fine
  • Changing the kernel when running the notebook to the conda kernel: also fine
  • I checked out the code and saw it uses nbconvert under the hood. So I tried: jupyter nbconvert --to=html --execute 00_core.ipynb and that was fine.

The error seems to indicate that whatever temporary environment the code is using does not use the same python path as the rest of my environment.

I’ve run out of ideas… am I missing something obvious?

Thanks,
David.

Figured it out! Hopefully this will be useful to future searchers. The problem was that the kernel in this environment was installed under the name python3.9. The code is hardcoded to look for a kernel called python3. I’m actually surprised there wasn’t a no such kernel error (or maybe I had a kernel named python3 installed in some other environment?)

The immediate solution is:

ipython kernel install --user

(as opposed to something like ipython kernel install --user --name python3.9 which results in a name other than python3.)

Another solution which worked was to use the kernel defined by the kernelspec of the notebook. In my mind this makes sense as this was the kernel used during the notebook execution. If you pass no parameter to nbconvert then it has this behavior by default: https://github.com/jupyter/nbclient/blob/master/nbclient/client.py#L181. I’m wondering why the choice was made to pass a kernel instead of using the one in the notebook?

I’ll open a PR to use the kernel specified in the notebook and see what you think.

PR: https://github.com/fastai/nbdev/pull/594

Hope this helps!

1 Like