[nbdev] Can't import the module I made from subfolder

Hello, everyone!

I started developing my own project named nbdev_school using nbdev.

First, I created a dir named nbs to place all notebooks.
Second, created a notebook named student.ipynb to declare Student class.
Third, I exported it to nbdev_school/student using nbdev_export command.
Finally, I tried to import the student module in index.ipynb to build my school.

However, when running from nbdev_school.student import * in index.ipynb,

I got an error below.

ModuleNotFoundError: No module named 'nbdev_school'

I guess the reason of the error above is that we can’t import Student inside nbs dir.

Is there a nice way to handle this error?
(The easiest solution is to move all notebooks to top level, but I’d like to keep it organazed like fast.ai repo).

My repo structure is below.

(and more)

Thanks in advance.

did you execute nbdev_install ? Your python system needs to be aware of the new library, before you can use it. Even for itself, it seems. (I think nbdev v1 could be used without install, but not sure that’s true for nbdev v2)

1 Like

Thank you @michaelaye!
nbdev_install is what I need. Now I can import my module.

Note for me: It installs Quarto and updates base path. And top_level.txt and some files will be created in nbdev_school.egg-info directory.

IIRC we worked around this in v1 by putting a symlink in the notebooks folder.

Thank you @jeremy!

I found a symbolic link in the notebooks folder at nbdev v1 repo

1 Like

Ah yes, if notebooks were in a sub folder it needed the symlink, but if they were in root, no install was required. Anyhow, most projects grow large enough to justify their own subfolder, don’t they?

I have essentially the same problem, but it isn’t solved by nbdev_install.

After exporting with nbdev_export I have tried:
pip install -e . (suggested in YouTube tutorial)
pip install -e ‘.[dev]’ (suggested in the “End-to-End Walkthrough”)
nbdev_install (suggested here, with Quarto installed)

None of those seems to make the import work.

I tried following the steps directly from the nbdev tutorial on YouTube for the nbdev_cards project, and can’t get that to work. The nbdev_new command creates the nbs dir in the main repo, and when I use nbdev_export the nbdev_card package is at the same level in the repo as nbs. Running index.ipynb within nbs gives the ModuleNotFoundError for the line “from nbdev_cards.card import *”

If I move index.ipynb to the top level of the repo, then the import works fine. However, if I take all the files from the nbs dir and put them at the top level of the repo then the export no longer works. Am I missing something?

Hello @bja, welcome to fast.ai forum!
It seems that you have a path issue. I guess your setting.ini is not set properly. Please check if the value “nbs” is set for “nbs_path” like official repo.
If this doesn’t help, if possible, please share your GitHub repo so we can look into where your problem is.

Hi @peter826, thanks for your help!

nbs is set for nbs_path in settings.ini

I’m guessing the problem might be related to the UserWarning I got when using nbdev_new to set up the project, but I am not sure. I haven’t figure that out yet, and an old post with a similar problem doesn’t seem to be resolved.

Update: Updated conda and anaconda, remade the project in a public repo, and ran nbdev_install_hooks again, and now the import seems to work!

1 Like

@bja Glad to hear that🤗
I had the same problem in an environment with proxies, so that may have something to do with it.