nbdev icon indicating copy to clipboard operation
nbdev copied to clipboard

ImportError: cannot import name 'strip_ansi' from 'IPython.utils.text' with Python 3.13 and recent IPython versions

Open dbikard opened this issue 8 months ago • 2 comments

When trying to install nbdev I encountered an import error when running nbdev_install_quarto in an environment with Python 3.13 and a recent version of IPython. The execnb.shell module attempts to import strip_ansi from IPython.utils.text, but this function has been moved to IPython.core.ultratb in IPython versions 8.0 and later.

This breaks functionality that relies on execnb.

Steps to Reproduce:

  1. Set up a Python 3.13 environment (e.g., using conda).
  2. Install jupyterlab pip install jupyterlab
  3. Install nbdev (which will pull execnb as a dependency).
pip install nbdev
  1. Attempt to run a command that utilizes execnb, for example:
nbdev_install_quarto

The following traceback occurs:

nbdev_install_quarto
Traceback (most recent call last):
  File "/home/dbikard/anaconda3/envs/randseq/bin/nbdev_install_quarto", line 5, in <module>
    from nbdev.quarto import install_quarto
  File "/home/dbikard/anaconda3/envs/randseq/lib/python3.13/site-packages/nbdev/quarto.py", line 18, in <module>
    from .serve import proc_nbs,_proc_file
  File "/home/dbikard/anaconda3/envs/randseq/lib/python3.13/site-packages/nbdev/serve.py", line 19, in <module>
    from .processors import FilterDefaults
  File "/home/dbikard/anaconda3/envs/randseq/lib/python3.13/site-packages/nbdev/processors.py", line 23, in <module>
    from execnb.shell import *
  File "/home/dbikard/anaconda3/envs/randseq/lib/python3.13/site-packages/execnb/shell.py", line 20, in <module>
    from IPython.utils.text import strip_ansi
ImportError: cannot import name 'strip_ansi' from 'IPython.utils.text' (/home/dbikard/anaconda3/envs/randseq/lib/python3.13/site-packages/IPython/utils/text.py)

Environment:

  • OS: Ubuntu
  • Python Version: 3.13
  • nbdev Version: 2.3.34
  • execnb Version: 0.1.11
  • IPython Version: 9.2.0

Proposed Solution (for execnb):

Update the import in execnb/shell.py to be compatible with modern IPython versions. This typically involves changing:

from IPython.utils.text import strip_ansi

to:

try:
    from IPython.utils.text import strip_ansi  # For older IPython versions
except ImportError:
    from IPython.core.ultratb import strip_ansi  # For IPython 8.0+

This will ensure compatibility across different IPython versions.

dbikard avatar May 20 '25 09:05 dbikard

I have now discovered that the problem depends installation order. The nbdev documentation/walkthrough suggests installing jupyterlab first, then nbdev:

pip install jupyterlab
pip install nbdev
nbdev_install_quarto

When following this order, I consistently hit the ImportError as previously discussed.

However, if I skip the pip install jupyterlab step and directly pip install nbdev in a clean Python 3.11 environment, then the nbdev_install_quarto command works.

Note that the End-To-End Walkthrough as written now doesn't work (at least in my setup), so this feels like a pretty urgent problem to fix.

dbikard avatar May 20 '25 09:05 dbikard

I now see that this was resolved in more recent versions of execnb. Things work fine with execnb 0.1.14 and with nbdev 2.4.2. I unsure why pip installed nbdev Version: 2.3.34 and execnb Version: 0.1.11 in my previous attempts, but it doesn't seem to be an issue now.

dbikard avatar May 20 '25 11:05 dbikard

Great thanks for the update.

jph00 avatar Jul 02 '25 00:07 jph00

Faced the same issue with execnb when installing via conda since it installs 0.1.11 version. using pip install fixed it since it installed 0.1.14.

fzngagan avatar Sep 08 '25 09:09 fzngagan