ModuleNotFoundError: No module named 'nbformat'

Question:

I would like to run python in a Quarto document. I followed the docs about installing and using python in Quarto, but the error stays. Here is some reproducible code:

---
title: "matplotlib demo"
format:
  html:
    code-fold: true
jupyter: python3
---

For a demonstration of a line plot on a polar axis, see @fig-polar.

```{python}
#| label: fig-polar
#| fig-cap: "A line plot on a polar axis"

import numpy as np
import matplotlib.pyplot as plt

r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r
fig, ax = plt.subplots(
  subplot_kw = {'projection': 'polar'} 
)
ax.plot(theta, r)
ax.set_rticks([0.5, 1, 1.5, 2])
ax.grid(True)
plt.show()
``` 

Error output:

Starting python3 kernel...Traceback (most recent call last):
  File "/Applications/RStudio.app/Contents/Resources/app/quarto/share/jupyter/jupyter.py", line 21, in <module>
    from notebook import notebook_execute, RestartKernel
  File "/Applications/RStudio.app/Contents/Resources/app/quarto/share/jupyter/notebook.py", line 16, in <module>
    import nbformat
ModuleNotFoundError: No module named 'nbformat'

I also checked with Quarto if Jupyter is installed in the terminal like this:

quarto check jupyter

Output:

[✓] Checking Python 3 installation....OK
      Version: 3.7.11 (Conda)
      Path: /Users/quinten/Library/r-miniconda/envs/r-reticulate/bin/python
      Jupyter: 4.12.0
      Kernels: julia-1.8, python3

[✓] Checking Jupyter engine render....OK

Which seems to be OK. So I was wondering if anyone knows how to fix this error?


Edit: output conda info –envs

Output of conda info:

# conda environments:
#
                         /Users/quinten/.julia/conda/3
                         /Users/quinten/Library/r-miniconda
                         /Users/quinten/Library/r-miniconda/envs/r-reticulate
                         /Users/quinten/Library/rminiconda/general
                         /Users/quinten/opt/anaconda3
base                  *  /Users/quinten/opt/miniconda3

Edit: conda install Jupyter

The condo install Jupyter was installed (thanks to @shafee), now when I check with quarto if Jupyter exists, I get the following error:

quarto check jupyter

[✓] Checking Python 3 installation....OK
      Version: 3.7.11 (Conda)
      Path: /Users/quinten/Library/r-miniconda/envs/r-reticulate/bin/python
      Jupyter: 4.11.1
      Kernels: julia-1.8, python3

(/) Checking Jupyter engine render....Unable to load extension: pydevd_plugins.extensions.types.pydevd_plugin_pandas_types
Unable to load extension: pydevd_plugins.extensions.types.pydevd_plugin_pandas_types
[✓] Checking Jupyter engine render....OK
Asked By: Quinten

||

Answers:

This error usually occurs when jupyter is not installed in the environment that is being used and from the output of quarto checks, its seems that Quarto is referring to the r-reticulate environment and possibly jupyter is not installed in that environment. If thats the case, you simply need to install jupyter in the r-reticulate environment.

Option 01

One option could be to activate the r-reticulate environment and then install the jupyter there.

So first, activate the environment.

conda activate r-reticulate

and then install jupyter there,

conda install jupyter

Option 02

If conda could not find that environment, use the specific path to that environment to install jupyter in that environment,

/Users/quinten/Library/r-miniconda/envs/r-reticulate/bin/python -m pip install jupyter

Option 03

If the r-reticulate environment is created by the {reticulate} R package, then an easy option could be using reticulate::conda_install to install a package.

reticulate::conda_install(envname = "r-reticulate", "jupyter")

Also, if you are wondering, why jupyter command runs even though it did not exist in the r-reticulate environment, its because you probably installed jupyter in the base environment and quarto is detecting that jupyter installation while checking.

Answered By: shafee
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.