【问题标题】:TypeError when executing jupyter nbconvert执行 jupyter nbconvert 时出现 TypeError
【发布时间】:2016-08-02 14:51:48
【问题描述】:

我在运行 jupyter nbconvert 时遇到问题。

它总是显示以下错误跟踪(即使是带有几个普通单元格的笔记本)

Traceback (most recent call last):
  File "/home/miguel/.virtualenvs/ipython/bin/jupyter-nbconvert", line 11, in <module>
    sys.exit(main())
  File "/home/miguel/.virtualenvs/ipython/local/lib/python2.7/site-packages/jupyter_core/application.py", line 267, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/home/miguel/.virtualenvs/ipython/local/lib/python2.7/site-packages/traitlets/config/application.py", line 596, in launch_instance
    app.start()
  File "/home/miguel/.virtualenvs/ipython/local/lib/python2.7/site-packages/nbconvert/nbconvertapp.py", line 293, in start
    self.convert_notebooks()
  File "/home/miguel/.virtualenvs/ipython/local/lib/python2.7/site-packages/nbconvert/nbconvertapp.py", line 447, in convert_notebooks
    self.exporter = cls(config=self.config)
  File "/home/miguel/.virtualenvs/ipython/local/lib/python2.7/site-packages/nbconvert/exporters/templateexporter.py", line 151, in __init__
    super(TemplateExporter, self).__init__(config=config, **kw)
  File "/home/miguel/.virtualenvs/ipython/local/lib/python2.7/site-packages/nbconvert/exporters/exporter.py", line 101, in __init__
    self._init_preprocessors()
  File "/home/miguel/.virtualenvs/ipython/local/lib/python2.7/site-packages/nbconvert/exporters/exporter.py", line 250, in _init_preprocessors
    self.register_preprocessor(preprocessor, enabled=True)
  File "/home/miguel/.virtualenvs/ipython/local/lib/python2.7/site-packages/nbconvert/exporters/exporter.py", line 211, in register_preprocessor
    preprocessor_cls = import_item(preprocessor)
  File "/home/miguel/.virtualenvs/ipython/local/lib/python2.7/site-packages/ipython_genutils/importstring.py", line 31, in import_item
    module = __import__(package, fromlist=[obj])
TypeError: Item in ``from list'' not a string

这可能与我最近安装的 nbextensions 有关,因为几天前 jupyter nbconvert 命令曾经完美运行。

今天,我在安装 nbextensions 时遇到了一些问题。我尝试使用 conda,但当我按照here 的说明进行操作时,它终于奏效了。

【问题讨论】:

  • 您有~/.jupyter/jupyter_nbconvert_config.py(或.json)文件吗?

标签: jupyter jupyter-notebook nbconvert


【解决方案1】:

我今天也遇到了同样的问题。正如here 所记录的那样,这似乎是 nbconvert 中的一个错误。解决方案是编辑您的“jupyter_nbconvert_config.json”文件。对我来说,它默认位于我的主目录中:/home/me/.jupyter/jupyter_nbconvert_config.json

在那里,我更改了预处理器字符串,在它们前面添加了一个“r”:

{
  "Exporter": {
    "template_path": [
      ".", 
      "/usr/lib/python2.7/site-    packages/jupyter_contrib_nbextensions/templates"
    ], 
    "preprocessors": [
      r"jupyter_contrib_nbextensions.nbconvert_support.CodeFoldingPreprocessor", 
      r"jupyter_contrib_nbextensions.nbconvert_support.PyMarkdownPreprocessor"
    ]
  }, 
  "NbConvertApp": {
    "postprocessor_class":     "jupyter_contrib_nbextensions.nbconvert_support.EmbedPostProcessor"
  }, 
  "version": 1
}

【讨论】:

  • 将 r 添加到 JSON 配置中的字符串会导致 JSONDecodedError。这似乎有效(等待 ipython_genutils 的更新版本):github.com/jupyter/nbconvert/issues/362#issuecomment-246169144
  • 为我工作。我必须更改两个相同的 JSON 文件:一个在 ~/.jupyter/,另一个在 ~/anaconda/etc/jupyter。出于某种奇怪的原因,只更改一个文件不起作用。
【解决方案2】:

找到解决方案here。 基本上,您需要删除或重命名您的 jupyter_nbconvert_config 文件。

【讨论】:

    【解决方案3】:

    最近修复了这个bug,详情见here。运行 ipython 的完整更新,或者手动修复:

    更改 ipython_genutils/importstring.py 中的第 31 行

    module = __import__(package, fromlist=[obj])     
    

    module = __import__(package, fromlist=[str(obj)])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 2020-04-05
      相关资源
      最近更新 更多