1

I am using pytest nbmake in order to run unit tests on python notebooks. My notebooks work fine but when I try to run the unit tests I run into this error :

FAILED notebooks/analyze_curves.ipynb:: - ModuleNotFoundError: No module named 'computing'

My project structure looks like this:

enter image description here

And this is what my imports look like:

enter image description here

Since my notebooks run fine, I think the problem is from pytest nbmake not being able to import modules properly.

Does anyone knows how to fix this issue ?

anni
  • 1,403
  • 1
  • 24
  • 33

2 Answers2

0

I may have faced this one before. Before any imports in your notebook try adding the following:

import sys 
sys.path.append(‘../‘)

If my understanding is right it should move your modules search space one directory up so your notebook can locate your package. Hope this works!

Edit: If this doesn’t work, try using an absolute path to the directory your package is contained in, in place of ‘../‘.

Or you could compute this dynamically using something like this: https://stackoverflow.com/a/32838876/13944042

rexess
  • 729
  • 4
  • 7
0

As the answer above pointed out the problem is that your computing package is missing from the PYTHONPATH environment variable. You need to append the path of your project root to PYTHONPATH. If you are using anaconda as your environment manager you can easily solve it by running

conda develop "path/to/notebook-unit-test folder" 

If you are not using anaconda then you need to run the following command in the same console where you run the pytest command.

set PYTHONPATH=%PYTHONPATH%;"path/to/notebook-unit-test folder"

This only sets it temporarily, so the next time you open up your console you'll need to run it again.

Grinjero
  • 436
  • 2
  • 7