Problem with nbdev and notebook2script

Hi, I have a problem with nbdev and notebook2script . I have installed the following versions: nbdev 2.3.6; notebook2script 0.2.1.
When I try to run from nbdev.export import notebook2script I get the following error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Input In [30], in <cell line: 1>()
----> 1 from nbdev.export import notebook2script

ImportError: cannot import name 'notebook2script' from 'nbdev.export' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/nbdev/export.py)

Has anyone solved this problem?
Thanks

2 Likes

You may want to refer the migration guide especially update directive names nbdev - nbdev1 Migration

As suggested here:

import nbdev
nbdev.export.nb_export('app.ipynb', 'app')
print('Export successful')
12 Likes

Thank you @chrwittm, I had trouble with that! :slightly_smiling_face:
I’ll add that the 2nd parameter to nb_export is the directory path and not the filename, so if you wish to create the Python file directly in the same dir as your notebook, you can do this:
nbdev.export.nb_export('app.ipynb', './')

12 Likes

Documenting an error I ran into.

You might see
ExportModuleProc._default_exp_() missing 1 required positional argument: 'exp_to' if you’ve called #|default_exp without a filename. So just run the cell again as #|default_exp app and then nbdev.export.nb_export('app.ipynb').

1 Like
# Export Notebook to python Script -- nbdev --
from notebook2script import convert_notebook
convert_notebook('Lesson-2.ipynb','app.py')

This is one of the solution I did found while experimenting

And I would like to add to @chrwittm and @dway8 comments that if it’s running without error but it’s not creating any .py file for you, it’s because you need to install JupyterLab Extension. If you are using pip you can run below:
pip install jupyterlab-quarto
Here is the link for other ways of its installation:

I have the same problem with notebook2script. Kaggle does not run it and locally it is not working after conda installations and pip installations.
The above solution did not work. to uninstall do

py -m pip uninstall jupyterlab-quarto

ImportError Traceback (most recent call last)
Cell In[126], line 1
----> 1 from nbdev.export import notebook2script
3 notebook2script(‘app.app.ipynb’)

ImportError: cannot import name ‘notebook2script’ from ‘nbdev.export’ (/opt/conda/lib/python3.10/site-packages/nbdev/export.py)

!jupyter nbconvert --to script app.ipynb tried this from kaggle but no luck.

This seems to export everything, not just those things marked with the export statement.

As of 24.3.18 what worked for me was:

  1. pip install nbdev
  2. Add #|default_exp INSERT_EXPORT_FILE_NAME to the top of your notebook (e.g. ‘app’ would export app.py)
  3. Add #|export to the top of each cell you want to export
  4. run a cell with the following code:
    import nbdev
    notebook_name = "notebook.ipynb"
    export_destination = "." # the root directory
    nbdev.export.nb_export(notebook_name, export_destination) 

Note:

  • Make sure that your notebook is saved or you might not export all the latest changes

Two more notes:

  1. Keep in mind what @elnazfathi points out above about needing to have jupyterlab-quarto installed
  2. Make sure that your changes are saved in whatever IDE you are working in.

I tried this in Google Colab. Didn’t work. Getting the error:

FileNotFoundError: [Errno 2] No such file or directory: 'app.ipynb'

Try replacing app.ipynb with the name of your notebook file. For example, if you have a notebook named lesson_2_notebook.ipynb then use that.

(I have edited my post above to clarify between the export file name and the notebook file name. I think that’s where the confusion may have been.)

Thanks @dynamics
Actually the issue was the export file name expected the whole file path in Google Colab.
So it was fixed using the following code:

path = Path('.')
path.ls()
gdrive = path / 'drive/MyDrive/Colab Notebooks'
gdrive.ls()
import nbdev
nbdev.export.nb_export(gdrive/'pet_classifier.ipynb', 'app')
print('Export successful')

Reference: Error with nbdev.export.nb_export

I’d one more issue, this exports whole notebook and not just the cells with #/export as seen in the lecture. Any clue on how to fix this?

Ahh, ok. I should have specified that I was working locally with the notebook file in my root directory, good point.

Do you have #|default_exp INSERT_EXPORT_FILE_NAME in the top of the first cell of your notebook? That’s the only thing I can think of without seeing the rest of you code.

If that doesn’t work I’d be happy to I’d be happy to take a look if you can send me a link to your notebook.